coding challenge #4

 



































































// function expression
const calcTip = function (bill) {
return bill <= 300 && bill >= 50 ? 0.15 * (bill) : 0.2 * (bill);
// This reads as if bill is less than equal to 300 And And if bill is greater than equal
to 50 , then 0.15* bill , else 0.2* bill.

}

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

const tips = [];

const totals = [];

for (let i = 0; i < bills.length; i++) {

tips.push(calcTip(bills[i]));

totals.push(bills[i] + tips[i]);

}

console.log(tips);
console.log(totals);

//BONUS

// Function Expression
const calcAverage = function (arr) {

let sum = 0;

for (let i = 0; i < arr.length; i++) {

sum += arr[i];
}

// This is the first way
// const average = sum / arr.length;
// console.log(average);

// This way is much more logical as we are returning

return sum / arr.length;

}

// calcAverage(totals); // This is for the first way

console.log(calcAverage(totals));








Comments