Coding Challenge 5
Remember the tip calculator challenge? Let's create a more advanced version using everything we learned!
This time, Harvey and his family went to 5 different restaurants. The bills were $124, $48, $268, $180 and $42.
Harvey likes to tip 20% of the bill when the bill is less than $50, 15% when the bill is between $50 and $200, and 10% if the bill is more than $200.
Implement a tip calculator using objects and loops:
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, create 1) a new array containing all tips, and 2) an array containing final paid amounts (bill + tip). HINT: Start with two empty arrays [] as properties and then fill them up in the loop.
EXTRA AFTER FINISHING: Jessica's family also went on a holiday, going to 4 different restaurants. The bills were $77, $375, $110, and $45.
Jessica likes to tip 20% of the bill when the bill is less than $100, 10% when the bill is between $100 and $300, and 25% if the bill is more than $300 (different than Harvey).
5. Implement the same functionality as before, this time using Jessica's tipping rules
6. Create a function (not a method) to calculate the average of a given array of tips. HINT: Loop over the array, and in each iteration store the current sum in a variable (starting from 0). After you have the sum of the array, divide it by the number of elements in it (that's how you calculate the average)
7. Calculate the average tip for each family
8. Log to the console which family paid the highest tips on average
*/
var harvey = {
fullName: 'Harvey Specter',
bills: [124, 48, 268, 180, 42],
calcTips: function () {
this.tips = []; //declara um array que irá receber as tips.
this.finalValues = []; //array com os valores finais das bills.
for (var i = 0; i < this.bills.length; i++) { //this.bills para mostrar que está nesse objeto.
// Determine percentage based on tipping rules.
var percentage;
var bill = this.bills[i];
if (bill < 50) {
percentage = .2;
} else if (bill >= 50 && bill < 200) {
percentage = .15;
} else {
percentage = .1;
} // .if
// Add results to the corresponding arrays.
this.tips[i] = bill * percentage;
this.finalValues[i] = bill + bill * percentage;
} // .for
} // .function
}
var jessica = {
fullName: 'Jessica Pearson',
bills: [77, 375, 110, 45],
calcTips: function () {
this.tips = []; //declara um array que irá receber as tips.
this.finalValues = []; //array com os valores finais das bills.
for (var i = 0; i < this.bills.length; i++) { //this.bills para mostrar que está nesse objeto.
// Determine percentage based on tipping rules.
var percentage;
var bill = this.bills[i];
if (bill < 100) {
percentage = .2;
} else if (bill >= 100 && bill < 300) {
percentage = .1;
} else {
percentage = .25;
} // .if
// Add results to the corresponding arrays.
this.tips[i] = bill * percentage;
this.finalValues[i] = bill + bill * percentage;
} // .for
} // .function
}
//essa function esta fora dos objetos pois se tivesssemos 90 objetos, iriamos repetr-la 90 vezes atoa. Entao fazemos uma vez e passamos os valores de cada objeto aqui dentro, e depois o resultado e adicionado ao objeto.
function calcAverage(tips) {
var sum = 0;
for(var i = 0; i < tips.length; i++) {
sum = sum + tips[i];
}
return sum / tips.length; //retornar a soma dividido pelo tamanho do array, para calcular o AVERAGE (MEDIA) que e soma tudo e divide por quantos somou.
} //se tivessemos [2, 6, 4] -> sum = 2 / sum = 2 + 6 / sum = 8 + 4 .:. sum = 12.
// Do the calculations
harvey.calcTips();
jessica.calcTips();
console.log(harvey, jessica);
//Calculate the average
harvey.average = calcAverage(harvey.tips); //calculate this using harvey.tips, por isso nao usa o this ali, porque essa funcao nao estaa dentro de um objeto!
jessica.average = calcAverage(jessica.tips);
console.log(harvey, jessica);
if (harvey.average > jessica.average) {
console.log(harvey.fullName + '\'s family pays higher tips, with an average of $' + harvey.average + '.');
} else if (jessica.average > harvey.average) {
console.log(jessica.fullName + '\'s family pays higher tips, with an average of $' + jessica.average + '.');
}