-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
40 lines (30 loc) · 1.03 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
const varObj = {
tip: 0
}
window.onload = () => {
document.querySelector('#calculate').onclick = calculateTip;
document.querySelector('#reset').onclick = resetValues;
const tips = document.querySelectorAll('.tip');
tips.forEach(tip => {
tip.addEventListener('click', handleTipClick);
})
}
function handleTipClick(e) {
varObj.tip = Number(e.target.textContent.split('%')[0]);
}
function calculateTip() {
const amount = Number(document.querySelector('#amount').value);
const people = Number(document.querySelector('#people').value);
if (!amount && !people) {
alert("Please enter values");
return;
}
const tip = (amount * varObj.tip) / 100;
const billPerPerson = (amount + tip) / people;
document.querySelector('#tipamount').innerText = tip / people;
document.querySelector('#totalamount').innerText = billPerPerson;
}
function resetValues () {
document.querySelector('#tipamount').innerText = 0;
document.querySelector('#totalamount').innerText = 0;
}