challenge #2

Jonas.io Udemy Javascript Fundamentals 2

by trentHarlem

HTML

<h2>
Coding Challenge #2
</h2> 

<p>
Steven is still building his tip calculator, using the same rules as before: Tip 15% of the bill if the bill value is between 50 and 300, and if the value is different, the tip is 20%.
</p>

<p>
1. Write a function 'calcTip' that takes any bill value as an input and returns the corresponding tip, calculated based on the rules above (you can check out the code from first tip calculator challenge if you need to). Use the function type you like the most. Test the function using a bill value of 100.<br>
2. And now let's use arrays! So create an array 'bills' containing the test data below.<br>
3. Create an array 'tips' containing the tip value for each bill, calculated from the function you created before.<br>
4. BONUS: Create an array 'total' containing the total values, so the bill + tip.
</p>


TEST DATA: 125, 555 and 44<br>
<p>
HINT: Remember that an array needs a value in each position, and that value can actually be the returned value of a function! So you can just call a function as array values (so don't store the tip values in separate variables first, but right in the new array) 😉
</p>


GOOD LUCK 😀

CSS

html {
  font: 1.1em system-ui;
}

JavaScript

/* let tip15 = bill * 0.15
let tip20 = bill * 0.20 */

const bills = [125, 555, 44]
const totals = []

const calcTip = bill => {
  if (bill >= 50 && bill <= 300) {
    tip = bill * 0.15
    totals.push(bill+tip)
    return tip
  } else {
    tip = bill * 0.20
    totals.push(bill+tip)
    return tip
  }
}

const tips = [calcTip(bills[0]), calcTip(bills[1]), calcTip(bills[2])]
//console.log(tips)

console.log(bills, tips, totals);
console.log([bills, tips, totals]);

//const totals = [bills[0] + tips[0], bills[1] + tips[1], bills[2] + tips[2]];

//console.log(`Your bill was $${bill}, you tipped $${tip}, the total is $${bill+tip}`)