How can i address parameters arrays so i can avoid getting undefined Error?

43 Views Asked by At

I'm not precisely sure about what causes this error in my code but I guess it's the poor approach of addressing a parameter in the resualtValues function. ** thank u all in advance.**

         const calculator = {
          sum:[],
          subtraction:[],
          multiplication:[],
           division:[],
    
         sumValues:function(num1,num2){
         this.sum.push({
                 num1: num1,
                 num2: num2,
                })
                  },
         subtractionValues:function(num1,num2){
                this.subtraction.push({
               num1: num1,
               num2: num2,
      })
       },
         multiplyValues:function(num1,num2){
          this.multiplication.push({
            num1: num1,
            num2: num2,
       })
       },
         divisionValues:function(num1,num2){
            this.division.push({
            num1: num1,
            num2: num2,
    })
        },
     resualtValues: function(){
       return this.sum.forEach(function(item){
          return item.num1 + item.num2
          })
       }
     }



    calculator.sumValues(25,4)
    calculator.subtractionValues(20,5)
    calculator.multiplyValues(5,6)
    calculator.divisionValues(45,5)
    console.log(calculator.resualtValues())

I wanted to define some functions so when I call them they pass the given parameters to the arrays that inhabited in calculator object so that the resualtValues function use these parameters and operate four basic math operations.

2

There are 2 best solutions below

4
Javaid On

You are trying to return the values in forEach function which is wrong I think. I didn't understand your question properly but here is the updated code which is returning the result array ( Containing all four answers ). Let me know if it's according to your need or not

const calculator = {
    sum: [],
    subtraction: [],
    multiplication: [],
    division: [],

    sumValues: function (num1, num2) {
      this.sum.push({
        num1: num1,
        num2: num2,
      });
    },
    subtractionValues: function (num1, num2) {
      this.subtraction.push({
        num1: num1,
        num2: num2,
      });
    },
    multiplyValues: function (num1, num2) {
      this.multiplication.push({
        num1: num1,
        num2: num2,
      });
    },
    divisionValues: function (num1, num2) {
      this.division.push({
        num1: num1,
        num2: num2,
      });
    },
    resualtValues: function () {
      let result = [];
      // **Sum Operation**
      this.sum.forEach(function (item) {
        result.push(item.num1 + item.num2);
      });
      // **Sub Operation**
      this.subtraction.forEach(function (item) {
        result.push(item.num1 - item.num2);
      });
      // **Mul Operation**
      this.multiplication.forEach(function (item) {
        result.push(item.num1 * item.num2);
      });
      // **Div Operation**
      this.division.forEach(function (item) {
        result.push(item.num1 / item.num2);
      });
      return result
    },
  };

  calculator.sumValues(25, 4);
  calculator.subtractionValues(20, 5);
  calculator.multiplyValues(5, 6);
  calculator.divisionValues(45, 5);
  console.log(calculator.resualtValues());
3
Carsten Massmann On

... or you can simply do:

console.log(
 {sum:[25+4],
  subtraction:[20-5],
  multiplication:[5*6],
  division:[45/5]}
);