Basic Arithmetic Operations

Published by Deep Xavier in

Write a function that does the following for the given values: add, subtract, divide and multiply. These are simply referred to as the basic arithmetic operations. The variables have to be defined, but in this challenge they will be defined for you. All you have to do is check the variables, do some string to integer conversions, use some if conditions, and apply the arithmetic operation.

Examples

operation("1",  "2",  "add" ) ➞ 3

operation("4",  "5",  "subtract") ➞ -1

operation("6",  "3",  "divide") ➞ 2

Notes

  • The numbers and operation are given as strings and should result in an integer value.
  • If the operation results in an ArithmeticException, then return Integer.MIN_VALUE (e.g. division by zero).
  • Division results will be rounded down to its integral part.
Watch a quick demo on how Edabit works.