-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
48 lines (41 loc) · 1.76 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
const billInput = document.querySelector('#bill-amount');
const cashInput = document.querySelector('#cash-given');
const btnCheck = document.querySelector('#check-btn');
const errorDiv = document.querySelector('#error-div');
const notesClassArray = document.querySelectorAll('.number');
btnCheck.addEventListener('click',validateInput);
const notes = [2000,500,100,20,10,5,1];
function showError(error) {
errorDiv.firstElementChild.innerText = error;
errorDiv.classList.add('show');
setTimeout(() => errorDiv.classList.remove('show'),4000);
}
function clearDenominations() {
for(let i = 0; i < notes.length; i++) {
notesClassArray[i].innerText = "";
}
}
function calcDenominations(billAmount,cashGiven) {
let balanceCash = cashGiven - billAmount;
for(i=0; i<notes.length; i++) {
let numberOfNotes = Math.trunc(balanceCash/notes[i]);
balanceCash = balanceCash % notes[i];
notesClassArray[i].innerText = numberOfNotes;
}
}
function validateInput() {
clearDenominations();
let billAmount = billInput.value;
let cashGiven = cashInput.value;
if(!billAmount || !cashGiven) {
showError('I am not a magician to spawn a dove out of nowhere.Give me something to work with.');
} else if(isNaN(billAmount) || isNaN(cashGiven)) {
showError('Are you from the parallel universe? cause people in this dimension are not capable of dealing with these amounts yet.');
} else if(billAmount < 0 || cashGiven < 0) {
showError('Am i a joke to you? How on earth did you end up with a negative amount?');
} else if(Number(cashGiven) < Number(billAmount)) {
showError("Nice try..! But that won't sell here.Pay the full amount.");
} else {
calcDenominations(billAmount,cashGiven);
}
}