简体   繁体   中英

How can i make this function work with other operators

I'm doing an exercise, this exercise is about making a simple on screen calculator, the task description specify to make a function add,multiply,subtract,divide that takes two numbers that will be evaluated.. but i check some live versions from other people who did the exercise and it accepts more than two numbers so perhaps they use an array.. i tried attempting to make one too but this one doesn't work with other operators like /=,*= and -= .. how can i make this work with these operators not just addition..

I'm not on the DOM work yet, i'm still trying to first make this add function work before i move to the HTML part so this is the only code i have right now incase you'll wonder where's the rest.

This one works when adding.

function add(arr){
  var ans = 0;
  for(var i=0;i<arr.length;i++){
    ans += arr[i];
  }

  return ans;
}
add([100,2000,3000]);

But if change to other operators such as -,/,* it only returns 0; this one is divide

 function divide(arr){
      var ans = 0;
      for(var i=0;i<arr.length;i++){
        ans /= arr[i];
      }

      return ans; // will return 0
    }
    divide([2000,1000]);

The issue is that you're starting with ans = 0 when you should be starting off with the first item in the array. And if you start off with the first item you should start your loop on the second. This should work for all cases but below I have implemented division as an example.

 function div(arr){ var ans = arr[0]; for(var i=1;i<arr.length;i++){ ans /= arr[i]; } return ans; } let answer = div([100,2000,3000]); console.log(answer); console.log(100/2000/3000) 

Basically you can just take a function for every operation in an object and then call this function again for each element of the array with Array#reduce

 var operators = { addition: function (a, b) { return a + b; }, subtraction: function (a, b) { return a - b; }, multiplication: function (a, b) { return a * b; }, }; function calculate(operand, values) { return values.reduce(operators[operand]); } console.log(calculate('addition', [40, 1, 1])); console.log(calculate('subtraction', [40, 1, 20])); console.log(calculate('multiplication', [1, 2, 3, 4, 5, 6, 7])); 

Your big mistake is that you are always multiplying or dividing by 0 wich will affect athe whole result.

You should initialize the result to the first array element:

 function divide(arr){
      var ans = arr[0];
      for(var i=1;i<arr.length;i++){
        ans /= arr[i];
      }

      return ans; // will return 0
    }
    divide([2000,1000]);

But I whould recommend using .reduce() it will avoid having this problem:

     function divide(arr){
          return arr.reduce(function(a, b){
                return a/b;
          });
      }

Demo:

  function divide(arr){ return arr.reduce(function(a, b){ return a/b; }); } console.log(divide([2000,1000, 10])); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM