如何使函数像calculate(x,y,operator)?

时间:2017-08-18 18:48:39

标签: javascript function calculator

如何使这种功能起作用?如果我将其称为calculate(2, 4, '*'),则会返回字符串'2*4'。我也试过parseInt没有成功,它只是首先出现在String to Number。

function calculate(x, y, operation) {
  this.number = x + operation + y;
  return this.number;
}

4 个答案:

答案 0 :(得分:4)

只需使用想要的运算符创建一个对象,测试运算符是否存在,然后使用它。



var operators = {
        '+': function (a, b) { return a + b; },
        '-': function (a, b) { return a - b; },
        '*': function (a, b) { return a * b; },
        '/': function (a, b) { return a / b; }
    },
    calculate = function (val1, val2, sign) {
        if (sign in operators) {
            return operators[sign](val1, val2);
        }
    }

console.log(calculate(6, 7, '*'));




答案 1 :(得分:1)

这是一个基本的计算器,你已经编写了所有的代码。只是错过了评估。

注意:请注意,对于简单的计算器,可以使用eval。如果您出于安全原因不在某个关键项目等中实现此功能,但是如果它用于某些内部用途或用于分配,那么它就不那么糟糕且非常方便。



function calculator(x,y,operation){
  this.number=x+operation+y;
  return eval(this.number);
}
console.log(calculator(1,2,"+"));
console.log(calculator(1,2,"-"));
console.log(calculator(1,2,"*"));
console.log(calculator(1,2,"/"));




答案 2 :(得分:1)

您的operation参数只是一个字符串,而不是一个真正的运算符。这就是为什么它不能像你想的那样工作。所以,你有一些选择。

使用Eval

您可以使用eval函数以一种代码的形式运行String,这样:

function calculate(x, y, operation) {
  return eval(x + operation + y)
}

虽然它看起来如此简洁明了,但请记住,对于安全性 manutenibility ,这通常是不良做法性能原因(more about it here)。

使用条件

有时最简单的想法是最好的选择:只使用一些条件。

function calculate(x, y, operation) {
  if (operation === '+') return x + y
  else if (operation === '-') return x - y
  else if (operation === '*') return x * y
  else if (operation === '/') return x / y
  else if (operation === '%') return x % y
  else if (operation === '**') return x ** y
  else return NaN
}

它可能看起来有点重复,但请记住,您没有很多算术运算要处理。事实上,上面处理了all the binary arithmetic operators

另一种语法是switch case语句,但它只是一个更详细的条件结构,ideia保持不变。

使用具有函数的对象

另一种强烈受Nina Scholz' answer启发的方法是在Object的帮助下将运算符的String表示与它们等效的函数映射:

function calculate(x, y, operation) {
  var operators = {
    '+': function (a, b) { return a + b },
    '-': function (a, b) { return a - b },
    '*': function (a, b) { return a * b },
    '/': function (a, b) { return a / b },
    '%': function (a, b) { return a % b },
    '**': function (a, b) { return a ** b }
  }

  return operation in operators ? operators[operation](x, y) : NaN
}

如果在 ES2015 + 环境(see the compatibility)上,箭头功能可以使它非常优雅:

function calculate(x, y, operation) {
  const operators = {
    '+': (a, b) => a + b,
    '-': (a, b) => a - b,
    '*': (a, b) => a * b,
    '/': (a, b) => a / b,
    '%': (a, b) => a % b,
    '**': (a, b) => a ** b
  }

  return operation in operators ? operators[operation](x, y) : NaN
}

虽然我个人仍然认为简单的条件语句足以满足这种情况,但这种Object方法非常有趣,可以很好地展示JavaScript的灵活性。

答案 3 :(得分:0)

你应该把它分成4种不同的情况:

calculate: function(x,y,operation){
      switch(operation){
         case "*":
           this.number = x*y;
           break;
         case "/":
           this.number=x/y;
           break;
         case "+":
           this.number= x+y;
           break;
         case "-":
           this.number= x-y;
           break;
         default:
            break;
      }

        return this.number;
}

它也很容易扩展......

相关问题