-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
40 lines (34 loc) · 1.07 KB
/
main.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 billElement = document.getElementById("billAmount");
const totalBillElement = document.getElementById("totalBillAmount");
const tipElement = document.getElementById("tipAmount");
const noPpl = document.getElementById("noOfPeople");
billElement.addEventListener("keyup", calculateBill);
tipElement.addEventListener("keyup", calculateBill);
addButton.addEventListener("click", addCount);
subButton.addEventListener("click", subCount);
function calculateBill() {
let tipAmount = Number(tipElement.value);
let pplCount = Number(noPpl.textContent);
let billAmount = Number(billElement.value);
if (isNaN(billAmount) || billAmount === 0) {
return;
}
if (isNaN(tipAmount) || tipAmount === 0) {
tipAmount = 10;
}
totalBillElement.value = (billAmount + tipAmount) / pplCount;
}
function subCount() {
let pplCount = Number(noPpl.textContent);
if (pplCount > 1) {
pplCount--;
}
noPpl.textContent = pplCount;
calculateBill();
}
function addCount() {
let pplCount = Number(noPpl.textContent);
pplCount++;
noPpl.textContent = pplCount;
calculateBill();
}