| Viewed 63 times

How I can make Basic Calculator in JavaScript?


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

1 Answer
Mohammad Qandeel
Answered
16

This is a very cool function you can try

function calculator(num1,operator,num2) {
 return {
  '+': num1 + num2,
  '-': num1 - num2,
  '*': num1 * num2,
  '/': num2 ? num1 / num2 : 'Cannot divide by 0!'
 }[operator]
}

Example

calculator(5,'+',1) // 6

 


Related Questions