Hello guys, this is my first time posting for help, and it’s because I’ve seen that this problem has already been solved, but I want to know why the code I wrote doesn’t work only with one specific example, I mean, I’m doing the last test of the JavaScript course’s cash register and when I pass the following parameters: ‘checkCashRegister(3.26, 100, [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”, 100]]);’, it doesn’t return the expected result, I would like to know if they can help me please, and forgive me if I’m overlooking something, below is my code:
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var totalCid = 0;
var changeGiven = [];
var denominations = [
{ name: "ONE HUNDRED", value: 100 },
{ name: "TWENTY", value: 20 },
{ name: "TEN", value: 10},
{ name: "FIVE", value: 5 },
{ name: "ONE", value: 1 },
{ name: "QUARTER", value: 0.25 },
{ name: "DIME", value: 0.10 },
{ name: "NICKEL", value: 0.05 },
{ name: "PENNY", value: 0.01 }
];
for (var i = 0; i < cid.length; i++) {
totalCid += cid[i][1];
}
totalCid = Math.round(totalCid * 100) / 100;
if (change > totalCid) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
if (change === totalCid) {
return { status: "CLOSED", change: cid };
}
for (var j = 0; j < denominations.length; j++) {
var denomination = denominations[j];
var denominationTotal = 0;
while (change >= denomination.value && cid[j][1] >= denomination.value) {
change -= denomination.value;
cid[j][1] -= denomination.value;
denominationTotal += denomination.value;
change = Math.round(change * 100) / 100;
}
if (denominationTotal > 0) {
changeGiven.push([denomination.name, denominationTotal]);
}
}
if (change !== 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
return { status: "OPEN", change: changeGiven };
}
But when i use those parameters, the function give me back this:
{ status: ‘OPEN’,
change:
[ [ ‘ONE’, 90 ],
[ ‘QUARTER’, 6.5 ],
[ ‘DIME’, 0.2 ],
[ ‘PENNY’, 0.04 ] ] }
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 OPR/94.0.0.0
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: