← Back to challenges

Basic Arithmetic Operations

JavaScriptHardconditionslanguage_fundamentalsmath

Instructions

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.

The numbers and operation are given as strings and should result in an integer value.

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 Infinity, then return "undefined" (e.g. division by zero).
  • Division results will be rounded down to their integral parts.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: RegEx: Character Classes XII ⁠- \t