Value is not appended in the tipArray and totalArray though code is syntactically right and i think logically as well

Viewed 22

Please help me The question is to calculate tip and total amount paid by a man using object and loop. bill Amount given : 124, 48, 268, 180, 42 (all id $) Though it doesn't matter

  1. Create an object with an array for the bill values
  2. Add a method to calculate the tip
  3. This method should include a loop to iterate over all the paid bills and do the tip calculations
  4. As an output, crater 1 array containing all tips and another array containing final paid amount.
let billValue = {
  tipArray: [],
  totalArray: [],
  bill: [124, 48, 268, 180, 42],
  tipCalc: function() {
    for (let i = 0; i < this.bill.length(); i++) {
      switch (true) {
        case this.bill[i] < 50:
          tipValue = (20 / 100) * bill[i];
          totalValue = tipValue + this.bill[i];
          tipArray.push(tipValue);
          totalArray.push(totalValue);
          break;
        case this.bill[i] > 50 && this.bill[i] < 200:
          tipValue = (15 / 100) * bill[i];
          totalValue = tipValue + this.bill[i];
          tipArray.push(tipValue);
          totalArray.push(totalValue);
          break;
        case this.bill[i] > 200:
          tipValue = (10 / 100) * bill[i];
          totalValue = tipValue + this.bill[i];
          tipArray.push(tipValue);
          totalArray.push(totalValue);
          break;
        default:

      }
    }
  }

}
console.log(billValue.tipArray);
console.log(billValue.totalArray);

1 Answers

The length of an array is not a function, but a property.

for (let i = 0; i < this.bill.length; i++)

You also need to call your tipCalc function before logging the arrays.

billValue.tipCalc();
console.log(billValue.tipArray);
console.log(billValue.totalArray);

You also need to use this to qualify access to properties of the object and enclose all your switch cases in blocks as variables cannot be redeclared in the same block with let. The corrected code is as follows:

let billValue = {
  tipArray: [],
  totalArray: [],
  bill: [124, 48, 268, 180, 42],
  tipCalc: function() {
    for (let i = 0; i < this.bill.length; i++) {
      switch (true) {
        case this.bill[i] < 50:{
          let tipValue = (20 / 100) * this.bill[i];
            let totalValue = tipValue + this.bill[i];
          this.tipArray.push(tipValue);
          this.totalArray.push(totalValue);
          break;}
        case this.bill[i] > 50 && this.bill[i] < 200:{
          let tipValue = (15 / 100) * this.bill[i];
          let totalValue = tipValue + this.bill[i];
          this.tipArray.push(tipValue);
          this.totalArray.push(totalValue);
          break;}
        case this.bill[i] > 200:{
          let tipValue = (10 / 100) * this.bill[i];
          let totalValue = tipValue + this.bill[i];
          this.tipArray.push(tipValue);
          this.totalArray.push(totalValue);
          break;}
        default:

      }
    }
  }

}
billValue.tipCalc();
console.log(billValue.tipArray);
console.log(billValue.totalArray);
Related