📜  计算器函数 JS Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:27.583000             🧑  作者: Mango

代码示例1
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,"+"))