-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
38 lines (33 loc) · 1.24 KB
/
app.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
const billAmount = document.querySelector("#bill-amount");
const cashGiven = document.querySelector("#cash-given");
const checkButton = document.querySelector("#check-button");
const message = document.querySelector("#error-message");
const noOfNotes = document.querySelectorAll(".no-of-notes");
const availableNotes = [2000, 500, 100, 20, 10, 5, 1];
checkButton.addEventListener("click", function validateBillAndCashAmount() {
hideMessage();
if (billAmount.value > 0) {
if (cashGiven.value >= billAmount.value) {
const amountToBeReturned = cashGiven.value - billAmount.value; // 2022 - 12 = 2010
calculateChange(amountToBeReturned);
} else {
showMessage("Do you wanna wash plates?");
}
} else {
showMessage("Invalid Bill Amount");
}
});
function calculateChange(amountToBeReturned) {
for (let i = 0; i < availableNotes.length; i++) {
const numberOfNotes = Math.trunc(amountToBeReturned / availableNotes[i]);
amountToBeReturned = amountToBeReturned % availableNotes[i];
noOfNotes[i].innerText = numberOfNotes;
}
}
function hideMessage() {
message.style.display = "none";
}
function showMessage(msg) {
message.style.display = "block";
message.innerText = msg;
}