In order to streamline the process of calculating tip percentages based on bill amounts, I have developed a method within the object itself. However, to prevent redundancy, I have also created a separate function called calculateTip and a dedicated for loop to iterate over the bills stored in the John object.
After successfully computing individual tips and storing them in the tips array, my next goal is to add each original bill amount to its corresponding tip and push the result to the finalBills array.
This means that the finalBills array should display the total sums for each set of bills and their respective tips: [142.6, 57.60, etc...]
Here is what I have accomplished so far:
var john = {
patron: 'John',
bills: [
124,
48,
180,
268,
42
],
tips: [],
finalBills: []
}
function calculateTip(bill) {
if (bill < 50) {
percentage = (20 / 100);
} else if (bill >= 50 && bill < 200) {
percentage = (15 / 100);
} else {
percentage = (10 / 100);
}
return percentage * bill;
};
// console.log(john.bills.length);
for (var i = 0; i < john.bills.length; i++) {
var bill = john.bills[i];
console.log(bill);
var tips = calculateTip(bill);
var roundedTips = tips.toFixed(2);
john.tips.push(roundedTips);
console.log('These are the tip amounts: ', roundedTips)
var finalBill = (roundedTips + bill);
console.log('Final amounts: ', finalBill)
};
console.log(john)