-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (73 loc) · 2.32 KB
/
script.js
File metadata and controls
77 lines (73 loc) · 2.32 KB
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
const billInput=document.querySelector("#bill-input");
const billBtn=document.querySelector("#bill-verify");
const cashDiv=document.querySelector(".cash-div")
const cashInput=document.querySelector("#cash-input");
const cashBtn=document.querySelector("#cash-verify");
const messageDiv=document.querySelector(".main-message");
const message=document.querySelector("#output-short");
const outputDiv=document.querySelector(".main-output")
const noteQuantity=document.querySelectorAll(".note-quantity");
const availableCash=[2000,500,100,20,10,5,1];
const showCashDiv=()=>{
cashDiv.style.display="block";
}
const hideCashDiv=()=>{
cashDiv.style.display="none";
}
const showMessageDiv=()=>{
messageDiv.style.display="block";
}
const hideMessageDiv=()=>{
messageDiv.style.display="none";
}
const showOutputDiv=()=>{
outputDiv.style.display="block";
}
const hideOutputDiv=()=>{
outputDiv.style.display="none";
}
const showMessage=(msg)=>{
showMessageDiv();
message.innerText=msg;
}
const processor=(amount)=>{
for(let i=0;i<availableCash.length;i++){
let number=Math.trunc(amount/availableCash[i]);
amount = amount%availableCash[i];
noteQuantity[i].innerText=number;
}
}
const billHandler=()=>{
let billAmt=billInput.value;
if(billAmt>0){
billBtn.style.display="none";
showCashDiv();
hideMessageDiv();
}else{
showMessage("Please enter proper bill amount (negative values and empty values not allowed)");
}
}
const cashHandler=()=>{
let cashAmt=Number(cashInput.value);
let billAmt=Number(billInput.value);
if(cashAmt>0 && billAmt>0){
let finalAmt=parseInt(cashAmt-billAmt);
if(finalAmt>0){
showMessage(`The change returned is ${finalAmt}.See the table below for more info`);
processor(finalAmt);
showOutputDiv();
}else if(finalAmt===0){
showMessage(`The change returned is ${finalAmt}.`)
hideOutputDiv()
}
else {
showMessage("Please enter cash amount atleast equal to the bill amount.Try again");
hideOutputDiv();
}
}else{
showMessage("Please enter proper bill amount (negative values and empty values not allowed)");
hideOutputDiv();
}
}
billBtn.addEventListener("click",billHandler);
cashBtn.addEventListener("click",cashHandler);