-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtip_calc.js
59 lines (40 loc) · 1.46 KB
/
tip_calc.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
const billInput= document.getElementById('bill_input')
const tip_pct= document.getElementById('tip')
const people= document.getElementById('number_of_people')
const tip_amt= document.getElementById('tip_per_person')
const total_amt= document.getElementById('total_per_person')
let totalPeople= Number(people.innerText)
// calculateBill() calculates tip/person and total/person
const calculateBill= () => {
const bill= Number(billInput.value)
const tipAmt= (Number(tip_pct.value)/100)*bill
const tipPerPerson= tipAmt/totalPeople
const billPerPerson= (bill+tipAmt)/totalPeople
tip_amt.value= tipPerPerson.toFixed(2)
total_amt.value= billPerPerson.toFixed(2)
}
// increasePeople() increases the number of total people by one
const increasePeople= () => {
totalPeople += 1
people.innerText= totalPeople
calculateBill()
}
// decreasePeople() decreases the number of people by one
const decreasePeople= () => {
if(totalPeople <= 1){
// totalPeople= 1
return
}
totalPeople -= 1
people.innerText= totalPeople
calculateBill()
}
// resetAll() sets all fields to empty and number of people to one
const resetAll= () =>{
totalPeople= 1
document.getElementById('bill_input').value= ""
document.getElementById('tip').value= ""
document.getElementById('number_of_people').innerText= "1"
document.getElementById('tip_per_person').value= ""
document.getElementById('total_per_person').value= ""
}