← Back to challenges

Basic Calculator

JavaScriptHardalgebramathnumbers

Instructions

Create a function that takes two numbers and a mathematical operator + - / * and will perform a calculation with the given numbers.

Examples

calculator(2, "+", 2) ➞ 4

calculator(2, "*", 2) ➞ 4

calculator(4, "/", 2) ➞ 2

Notes

If the input tries to divide by 0, return: "Can't divide by 0!"

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Folding a Piece of Paper