In the below code when I console the values of calculateTips()
, I am getting the following error
Uncaught TypeError: Cannot set property '0' of undefined
let tipCal = {
bills: [124, 48, 268, 180, 42],
calculateTips: function() {
this.tips = [];
this.finalValue = [];
(this.bills).forEach(function(item, index) {
if (item < 50) {
this.tips[index] = (item * .2);
} else if (item >= 50 && item < 200) {
this.tips[index] = (item * .15);
} else {
this.tips[index] = (item * .1);
}
});
return this.tips;
}
}
I am unable to understand why I am getting this error because according to me I am doing the right thing. Please help.