I counldn’t get the answer for 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 should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}
.
But I got {status: “INSUFFICIENT_FUNDS”, change: }; instead.
Others work out fine.
function checkCashRegister(price, cash, cid) {
let totalChange = (cash - price).toFixed(2);
//total change that drawer should pay back
let finalCid = [];
console.log("change: "+ totalChange);
//totalCid - show given cid (element of cid 0-1)
let totalCid = 0;
for (let elem of cid){
totalCid += elem[1]; //just amount (number)
}
totalCid = totalCid.toFixed(2);
console.log(totalCid)
let currency = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.10,
"QUARTER": 0.25,
"ONE": 1.00,
"FIVE": 5.00,
"TEN": 10.00,
"TWENTY": 20.00,
"ONE HUNDRED": 100.00,
}
if(totalChange > totalCid){
return {status: "INSUFFICIENT_FUNDS", change: []};
} else if(totalChange === totalCid){
return {status: "CLOSED" , change: cid};
} else {
cid = cid.reverse(); //cid sorted in highest to lowest order
for(let elem of cid){
let cashInDrawer = [elem[0], 0];
while(totalChange >= currency[elem[0]] && elem[1] > 0){
cashInDrawer[1] += currency[elem[0]];
totalChange -= currency[elem[0]];
elem[1] -= currency[elem[0]];
}
if(cashInDrawer[1] > 0){
finalCid.push(cashInDrawer);
}
}
if(totalChange > 0){
return {status: "INSUFFICIENT_FUNDS", change: []};
}
return {status: "OPEN", change: finalCid};
}
}
console.log(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]]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: