-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
80 lines (74 loc) · 2.94 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
const billAmount = document.querySelector(".bill-amount");
const cashGiven = document.querySelector(".cash-given");
const btnCheck = document.querySelector(".btn-check");
const btnNext = document.querySelector(".btn-next");
const msgBillNotFilled = document.querySelector(".bill-not-filled");
const msgBillNotNumber = document.querySelector(".bill-not-number");
const msgBillNotGreaterThanZero = document.querySelector(
".bill-not-greater-than-zero"
);
const msgCashNotFilled = document.querySelector(".cash-not-filled");
const msgCashNotNumber = document.querySelector(".cash-not-number");
const msgCashNotGreaterThanBill = document.querySelector(
".cash-not-greater-than-bill"
);
const containerCash = document.querySelector(".container-cash");
const tableReturnBalance = document.querySelector(".table-return-balance");
const noOfNotes = document.querySelectorAll(".no-of-notes");
const availableNotes = [2000, 500, 100, 20, 10, 5, 1];
function changeWithMinimumNumberOfNotes(billAmount, cashGiven) {
let balance = cashGiven - billAmount;
const listNumberOfNotes = availableNotes.map(function (noteValue) {
const numberOfNotes = Math.trunc(balance / noteValue);
balance %= noteValue;
return numberOfNotes;
});
for (let i = 0; i < listNumberOfNotes.length; i++) {
noOfNotes[i].innerText = listNumberOfNotes[i];
}
}
function btnNextClickHandler() {
if (!msgBillNotFilled.classList.contains("hidden")) {
msgBillNotFilled.classList.add("hidden");
}
if (!msgBillNotNumber.classList.contains("hidden")) {
msgBillNotNumber.classList.add("hidden");
}
if (!msgBillNotGreaterThanZero.classList.contains("hidden")) {
msgBillNotGreaterThanZero.classList.add("hidden");
}
if (billAmount.value > 0) {
btnNext.classList.add("hidden");
containerCash.classList.remove("hidden");
} else if (billAmount.value === "") {
msgBillNotFilled.classList.remove("hidden");
} else if (isNaN(billAmount.value)) {
msgBillNotNumber.classList.remove("hidden");
} else {
msgBillNotGreaterThanZero.classList.remove("hidden");
}
}
function btnCheckClickHandler() {
if (!msgCashNotFilled.classList.contains("hidden")) {
msgCashNotFilled.classList.add("hidden");
}
if (!msgCashNotNumber.classList.contains("hidden")) {
msgCashNotNumber.classList.add("hidden");
}
if (!msgCashNotGreaterThanBill.classList.contains("hidden")) {
msgCashNotGreaterThanBill.classList.add("hidden");
}
if (cashGiven.value > Number(billAmount.value)) {
changeWithMinimumNumberOfNotes(billAmount.value, cashGiven.value);
tableReturnBalance.classList.remove("hidden");
} else if (cashGiven.value === "") {
msgCashNotFilled.classList.remove("hidden");
} else if (isNaN(cashGiven.value)) {
msgCashNotNumber.classList.remove("hidden");
} else {
msgCashNotGreaterThanBill.classList.remove("hidden");
}
}
btnNext.addEventListener("click", btnNextClickHandler);
btnCheck.addEventListener("click", btnCheckClickHandler);