Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Calculator Function JS Javascript

function calculate(number1,number2,operator){
if (!number1 || !number2){
    return "Invalid number"
}
if (!['*','-','/','+'].includes(operator)){
    return "Invalid operator"
}
switch(operator){
  case "+":
    return number1 + number2;
  case "/":
    return number1 * number2;
  case "*":
    return number1 * number2;
  case "-":
    return number1 - number2;
 }
}
console.log(calculate(5,6,"+"))
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Calculator #Function #JS #Javascript
ADD COMMENT
Topic
Name
3+9 =