2

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 Answers2

2

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());
Javaid
  • 320
  • 2
  • 9
  • How can I put and show my parameters into the arrays that I defined in the calculator object? I mean sum: [], subtraction: [], multiplication: [], division: [], – Rick sanchez Nov 12 '22 at 13:52
  • I used forEach function to make a loop so that If I call more than calculator.sumValues() functions for example, resualtValues: function () could return the result from all functions. meaning returning sum of the all parameters In the sum function in this case. – Rick sanchez Nov 12 '22 at 14:01
  • You got the main idea right but I just want to show the result in the arrays that i defined in the calculator object and by using forEach function i wanted to repeat the operation in how many times that a specific function with specific parameter has been called. – Rick sanchez Nov 12 '22 at 14:11
  • in other words in case a sum function has been called two times i want to not only four parameters pushed into the sum array but also sum up together and show the result in the resualtValues function. let me know if the explanation is still vague for you. – Rick sanchez Nov 12 '22 at 14:20
1

... or you can simply do:

console.log(
 {sum:[25+4],
  subtraction:[20-5],
  multiplication:[5*6],
  division:[45/5]}
);
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • How can I put and show my parameters into the arrays that I defined in the calculator object? I mean sum: [], subtraction: [], multiplication: [], division: [], – Rick sanchez Nov 12 '22 at 14:39
  • I just want to show the result in the arrays that i defined in the calculator object and by using forEach function i wanted to repeat the operation in how many times that a specific function with specific parameter has been called. – Rick sanchez Nov 12 '22 at 14:40
  • in other words in case a sum function has been called two times i want to not only four parameters pushed into the sum array but also sum up together and show the result in the resualtValues function. let me know if the explanation is still vague for you. – Rick sanchez Nov 12 '22 at 14:40