Tell us what’s happening:
all other tests are completed but only this is not getting fulfilled. 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]]) should return {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}.
Your code so far
function checkCashRegister(price, cash, cid) {
const currencyMap = {
PENNY: 0.01,
NICKEL: 0.05,
DIME: 0.1,
QUARTER: 0.25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
"ONE HUNDRED": 100
};
let changeDue = cash - price;
let drawerTotal = 0;
for (let i = 0; i < cid.length; i++) {
drawerTotal += cid[i][1];
}
if (changeDue > drawerTotal) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
if (changeDue === drawerTotal) {
return { status: "CLOSED", change: cid };
}
let changeToReturn = [];
for (let i = cid.length - 1; i >= 0; i--) {
let currency = cid[i][0];
let currencyValue = currencyMap[currency];
let currencyAmount = cid[i][1];
let currencyToReturn = 0;
while (changeDue >= currencyValue && currencyAmount > 0) {
currencyToReturn += currencyValue;
changeDue -= currencyValue;
currencyAmount -= currencyValue;
}
if (currencyToReturn > 0) {
changeToReturn.push([currency, currencyToReturn]);
}
}
if (changeDue > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
return { status: "OPEN", change: changeToReturn };
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
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:
JavaScript Algorithms and Data Structures Projects - Cash Register