0

im doing a exercise of bills, tips and total of the all.

const calcTip = function (bill) {
  return bill >= 50 && bill <= 300 ? bill * 0.15 : bill * 0.2;
};

const billsArray = [22, 295, 176, 440, 37, 105, 10, 1100, 86, 52];
const tipsArray = [];
const totalsArray = [];

for (let i = 0; i <= billsArray.length; i++) {
  const tip = calcTip(billsArray[i]);
  tipsArray.push(tip);
  totalsArray.push(tip + billsArray[i]);
}

console.log(billsArray, tipsArray, totalsArray);

It's all right but in the browser console said that last one is not a number? someone knows why? do i need to use float? enter image description here

MrDiamond
  • 958
  • 3
  • 17
vitoria
  • 37
  • 6

2 Answers2

3

it's because you didn't set condition <= exactly in for loop. it should be i<billsArray.length

David F
  • 605
  • 4
  • 10
1

It's because of out of array index i <= billsArray.length

can fixable with i < billsArray.length or i <= billsArray.length - 1

Thusithz
  • 736
  • 1
  • 12
  • 33