-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (46 loc) · 1.82 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* 🌟 APP: Tip Calculator */
const billInput = document.getElementById('billTotalInput')
const tipInput = document.getElementById('tipInput')
const numberOfPeopleDiv = document.getElementById('numberOfPeople')
const perPersonTotalDiv = document.getElementById('perPersonTotal')
// Get number of people from number of people div
let numberOfPeople = Number(numberOfPeopleDiv.innerText)
// ** Calculate the total bill per person **
const calculateBill = () => {
// get bill from user input & convert it into a number
const bill = Number(billInput.value)
// get the tip from user & convert it into a percentage (divide by 100)
const tipPercent = Number(tipInput.value) / 100
// get the total tip amount
const tipAmount = bill * tipPercent
// calculate the total (tip amount + bill)
const total = tipAmount + bill
// calculate the per person total (total divided by number of people)
const perPersonTotal = total / numberOfPeople
// update the perPersonTotal on DOM & show it to user
perPersonTotalDiv.innerText = `$${perPersonTotal.toFixed(2)}`
}
// ** Splits the bill between more people **
const increasePeople = () => {
// increment the amount
numberOfPeople += 1
// update the DOM with the new number of people
numberOfPeopleDiv.innerText = numberOfPeople
// calculate the bill based on the new number of people
calculateBill()
}
// ** Splits the bill between fewer people **
const decreasePeople = () => {
// guard clause
// if amount is 1 or less simply return
// (a.k.a you can't decrease the number of people to 0 or negative!)
if (numberOfPeople <= 1) {
return
}
// decrement the amount
numberOfPeople -= 1
// update the DOM with the new number of people
numberOfPeopleDiv.innerText = numberOfPeople
// calculate the bill based on the new number of people
calculateBill()
}