-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (40 loc) · 1.46 KB
/
index.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
const firstInputField = document.querySelector(".input-1");
const secondInputField = document.querySelector(".input-2");
const customNumber = document.querySelector(".custom-btn");
const resetBtn = document.querySelector(".reset-btn");
const tipAmount = document.querySelector(".amount");
const tipTotal = document.querySelector(".total");
const gridButtons = document.querySelector(".grid-btns");
function calcBill(amount, people) {
return (parseFloat(amount / people) + parseFloat(tipAmountPerPerson)).toFixed(
2
);
}
function calcTip(percentageNum, amount, people) {
tipAmountPerPerson = (((percentageNum / 100) * amount) / people).toFixed(2);
tipAmount.textContent = `$${tipAmountPerPerson}`;
tipTotal.textContent = `$${calcBill(amount, people)}`;
}
gridButtons.addEventListener("click", function (e) {
e.preventDefault();
const percentageNum = e.target.value;
const amount = firstInputField.value;
const numPeople = secondInputField.value;
if (e.target.classList.contains("click-btn")) {
if (amount === "" || numPeople === "") {
tipAmount.textContent = `$0.00`;
tipTotal.textContent = `$0.00`;
validateInputs([amount, numPeople]);
} else {
calcTip(percentageNum, amount, numPeople);
}
}
});
resetBtn.addEventListener("click", function (e) {
e.preventDefault();
tipAmount.textContent = "$0.00";
tipTotal.textContent = "$0.00";
firstInputField.value = "";
secondInputField.value = "";
customNumber.value = "";
});