-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtipCalculator.js
49 lines (41 loc) · 1.25 KB
/
tipCalculator.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
//functions used
// calculateBill()
// increasePeople()
// decreasePeople()
//id used
// "perPersonTotal"
// "numberOfPeople"
// "tipInput"
// "billTotalInput"
const perPersonTotalID = document.getElementById('perPersonTotal')
const numberOfPeopleID = document.getElementById('numberOfPeople')
const tipInputID = document.getElementById('tipInput')
const billTotalInputID = document.getElementById('billTotalInput')
let numberOfPeople = Number(numberOfPeopleID.innerText)
let error = document.getElementById('warn')
const calculateBill = () => {
const bill = Number(billTotalInputID.value)
console.log(bill)
const tipPErcent = Number(tipInputID.value)/100
const tipAmount = bill * tipPErcent
const total = tipAmount + bill
const totalPerPerson = total / numberOfPeople
perPersonTotalID.innerText = `$${totalPerPerson.toFixed(2)}`
}
const increasePeople = () => {
numberOfPeople += 1
numberOfPeopleID.innerText = numberOfPeople
calculateBill()
}
const decreasePeople = () => {
if (numberOfPeople <=1) {
error.style.visibility ="visible"
setTimeout(() => {
error.style.visibility ="hidden"
}, 5000);
return
}
numberOfPeople -= 1
numberOfPeopleID.innerText = numberOfPeople
calculateBill()
}