/*Psalms 37:21 The wicked borrow and do not repay, but the righteous give generously.*/
let price = 3.26;
let cid = [
['PENNY', 1.01],
['NICKEL', 2.05],
['DIME', 3.1],
['QUARTER', 4.25],
['ONE', 90],
['FIVE', 55],
['TEN', 20],
['TWENTY', 60],
['ONE HUNDRED', 100]
];
const denominations = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.1],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
]
const priceSpan = document.getElementById('price');
const cashPaid = document.getElementById('cash');
const changeDisplay = document.getElementById('change-due');
const screen = document.getElementById('screen');
const purchaseBtn = document.getElementById('purchase-btn');
let giveChange = false;
let hasRun = false;
priceSpan.innerText = `$${price}`;
let totalCid = Number(cid.reduce((acc, el) => el[1] + acc, 0)
.toFixed(2));
let balance = 0;
const stats = (bal, initTotalCid, initBal) => {
totalCid = Number(cid.reduce((acc, el) => el[1] + acc, 0)
.toFixed(2));
if((totalCid === 0 && bal === 0 && giveChange) || (totalCid === 0 && bal === 0 && giveChange && initBal === initTotalCid))
return '<strong>Status</strong>: CLOSED<br/>';
else if(bal == 0)
return '<strong>Status</strong>: OPEN<br/>';
else if( bal > 0)
return '<strong>Status</strong>: INSUFFICIENT_FUNDS';
else
return;
}
const hasSuitableLowerDenominations = (bal, change) => {
const changeArr = change.map((el) => [...el]);
let internalBalance = bal;
for (let i = (changeArr.length - 1); i >= 0; i--) {
if (denominations[i][1] < internalBalance) {
while (changeArr[i][1] > 0) {
internalBalance = Number((internalBalance - denominations[i][1]).toFixed(2));
changeArr[i][1] = Number((changeArr[i][1] - denominations[i][1]).toFixed(2));
}
}
if (internalBalance <= 0) return true;
}
return false;
}
const check = (paid) => {
balance = Number((paid - price).toFixed(2));
if(paid < price) {
alert('Customer does not have enough money to purchase the item');
return;
} else if(paid === price) {
changeDisplay.innerHTML = 'No change due - customer paid with exact cash';
return;
}
if(totalCid < balance || balance < 0 || hasSuitableLowerDenominations(balance, cid) == false)
giveChange = false;
else if(totalCid === balance)
giveChange = true;
else if(totalCid > balance)
giveChange = true;
balanceCalculator(paid);
}
const balanceCalculator = (paid) => {
let index = cid.length - 1;
let currentChange = cid.slice();
const ogBalance = Number((paid - price).toFixed(2));
const ogTotalCid = totalCid;
hasRun = true;
let denominationCount = {};
while(balance > 0 && index >= 0 && giveChange) {
while(balance >= denominations[index][1] && balance - denominations[index][1] >= 0 && currentChange[index][1] >= denominations[index][1] ) {
balance = Number((balance - denominations[index][1]).toFixed(2));
currentChange[index][1] = Number((currentChange[index][1] - denominations[index][1]).toFixed(2));
if(denominationCount[denominations[index][0]])
denominationCount[denominations[index][0]] ++;
else
denominationCount[denominations[index][0]] = 1;
}
if(index > 0)
index--;
}
cid = currentChange;
totalCid = Number(cid.reduce((acc, el) => el[1] + acc, 0)
.toFixed(2));
updateUI(denominationCount, ogTotalCid, ogBalance);
}
const updateUI = (obj, originalTotalCid, originalBalance) => {
changeDisplay.innerHTML = '';
screen.innerHTML = '';
if(hasRun) {
changeDisplay.innerHTML = stats(balance, originalTotalCid, originalBalance);
}
if(obj && giveChange)
Object.keys(obj).forEach((key)=> {
let value;
denominations.forEach((item, index) => {
if(key === item[0]) {
value = denominations[index][1];
return;
}
})
changeDisplay.innerHTML += `${key}: $${Number((value * obj[key]).toFixed(2))} `;
})
cid.forEach((el)=> {
const coinDiv = document.createElement('div');
coinDiv.innerHTML = `<strong>${el[0]}</strong> : $${el[1]}`;
screen.append(coinDiv);
})
const totalDiv = document.createElement('div');
totalDiv.innerHTML = `<strong>TOTAL</strong>: $${totalCid}`;
screen.append(totalDiv);
}
updateUI();
purchaseBtn.addEventListener('click', () => {
check(Number(cashPaid.value));
})
cashPaid.addEventListener('keydown', (e) => {
if(e.key === 'Enter')
check(Number(cashPaid.value));
})
console.log("\nTest #19");
price = 44.42;
document.querySelector("#cash").value = 60;
cid = [ [ 'PENNY', 0.88 ],
[ 'NICKEL', 1.35 ],
[ 'DIME', 2.6 ],
[ 'QUARTER', 0.75 ],
[ 'ONE', 0 ],
[ 'FIVE', 0 ],
[ 'TEN', 10 ],
[ 'TWENTY', 0 ],
[ 'ONE HUNDRED', 0 ] ];
document.querySelector("#purchase-btn").click();
console.log("actual", document.querySelector("#change-due").innerText);
console.log("expected", "Status: CLOSED TEN: $10 QUARTER: $0.75 DIME: $2.6 NICKEL: $1.35 PENNY: $0.88");
it seems to pass or is the problem formatting