-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
49 lines (42 loc) · 1.53 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
41
42
43
44
45
46
47
48
49
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) {
// 12
if (cashGiven.value >= billAmount.value) {
// 2022> 12 => true
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) {
// 2010
// go over all the available
for (let i = 0; i < availableNotes.length; i++) {
// no of notes need for the denomination
const numberOfNotes = Math.trunc(amountToBeReturned / availableNotes[i]);
// 2010 / 2000 = 1 || 10 / 500 = 0
// amount left after calculating the number of notes needed
amountToBeReturned = amountToBeReturned % availableNotes[i];
// 2010 % 2000 = 10 || 10 % 500 = 10
// updating the no of notes in the table for the current amount
noOfNotes[i].innerText = numberOfNotes;
}
}
function hideMessage() {
message.style.display = "none";
}
function showMessage(msg) {
message.style.display = "block";
message.innerText = msg;
}