-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
72 lines (56 loc) · 2.48 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
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
// linking from html world to javascript world
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 nextButton = document.querySelector("#nextBtn");
const cashGivenSection = document.querySelector('.cash-given-section')
const notesSection = document.querySelector('.notes-section')
// Total available notes stored in array
const availableNotes = [2000, 500, 100, 20, 10, 5, 1]
// adding event listener to button
checkButton.addEventListener("click", function validateBillAndCashAmount() {
hideMessage();
if (Number(billAmount.value) > 0) {
if (Number(cashGiven.value) >= Number(billAmount.value)) {
const amountToBeReturned = cashGiven.value - billAmount.value;
notesSection.style.display = 'block'
// calling calculateChange function which is core processing part
calculateChange(amountToBeReturned);
} else {
// calling showMessage function to show output to client
showMessage("Cash amount given should be greater than or equal to bill amount")
}
} else {
// calling showMessage function to show output to client
showMessage("Invalid bill amount");
}
});
// adding event listener to next button
nextButton.addEventListener("click", ()=> {
if (billAmount.value > 0){
cashGivenSection.style.display = 'block'
message.innerText = ""
} else {
showMessage("Invalid value") // calling a showMessage function
cashGivenSection.style.display = 'none'
notesSection.style.display = 'none'
}
});
// core processing behind this project
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 for showing or hiding output to client
function hideMessage() {
message.style.display = "none";
}
function showMessage(msg) {
message.style.display = "block";
message.innerText = msg;
}