Tell us what’s happening:
I have no idea why its not working. it is malfunctioning when returning what change is sent
Your code so far
let price = 19.5;
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]
];
function calculateCashInDrawer(cid) {
let cashInDrawer = 0;
for (const [denomination, amount] of cid) {
switch (denomination) {
case 'PENNY':
cashInDrawer += amount * 0.01;
break;
case 'NICKEL':
cashInDrawer += amount * 0.05;
break;
case 'DIME':
cashInDrawer += amount * 0.10;
break;
case 'QUARTER':
cashInDrawer += amount * 0.25;
break;
case 'ONE':
cashInDrawer += amount;
break;
case 'FIVE':
cashInDrawer += amount * 5;
break;
case 'TEN':
cashInDrawer += amount * 10;
break;
case 'TWENTY':
cashInDrawer += amount * 20;
break;
case 'ONE HUNDRED':
cashInDrawer += amount * 100;
break;
}
}
return cashInDrawer;
}
function changeBills(changeDue) {
let bills = [];
while (changeDue > 0) {
if (changeDue >= 100) {
bills.push(['One Hundred', '$' + (changeDue/100)*100]);
changeDue = changeDue % 100;
} else if (changeDue >= 20) {
bills.push(['Twenty', '$' + (changeDue/20)*20]);
changeDue = changeDue % 20;
} else if (changeDue >= 10) {
bills.push(['Ten', '$' + (changeDue/10)*10]);
changeDue = changeDue % 10;
} else if (changeDue >= 5) {
bills.push(['Five', '$' + (changeDue/5)*5]);
changeDue = changeDue % 5;
} else if (changeDue >= 1) {
bills.push(['One', '$' + (changeDue/1)*1]);
changeDue = changeDue % 1;
}
else if (changeDue >= 0.25) {
bills.push(['Quarter', '$' + (changeDue/0.25)*0.25]);
changeDue = changeDue % 0.25;
}
else if (changeDue >= 0.1) {
bills.push(['Dime', '$' + (changeDue/0.1)*0.1]);
changeDue = changeDue % 0.1;
}
else if (changeDue >= 0.05) {
bills.push(['Nickel', '$' + (changeDue/0.05)*0.05]);
changeDue = changeDue % 0.05;
}
else if (changeDue >= 0.01) {
bills.push(['Penny', '$' + (changeDue/0.01)*0.01]);
changeDue = changeDue % 0.01;
}
else {
break;
}
}
return bills;
}
function purchase(){
let cash = parseFloat(document.getElementById('cash').value);
let cashInDrawer= calculateCashInDrawer(cid);
if(price>cash){
alert("Customer does not have enough money to purchase the item");
console.log("Customer does not have enough money to purchase the item");
} else if(price===cash){
document.getElementById('change-due').innerHTML = 'No change due - customer paid with exact cash';
console.log("No change due - customer paid with exact cash");
}
else{
let changeDue=cash-price;
if (cashInDrawer<changeDue){
return "Status: INSUFFICIENT_FUNDS";
} else if (cashInDrawer===changeDue){
return "Status: CLOSED";
}else{
let bills = changeBills(changeDue);
document.getElementById('change-due').innerHTML = "Status:OPEN "+bills;
return "Status:OPEN "+bills;
}
}
}
Purchase
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register