Tell us what’s happening:
My algorithm gives the same output for all the test cases, even for the failling one.
Your code so far
function checkCashRegister(price, cash, cid) {
const currencies = [
0.01,
0.05,
0.1,
0.25,
1,
5,
10,
20,
100
];
let change = cash - price;
let changeArr = []
let total = cid.reduce((sum, item) => sum += item[1], 0)
let status = total > change? "OPEN" : total === change ? "CLOSED" : "INSUFFICIENT_FUNDS";
if(status === "INSUFFICIENT_FUNDS"){
return {status: "INSUFFICIENT_FUNDS", change: []}
}
if(status === "CLOSED")
{
return {
status : status,
change: cid
}
}
for(let i=8; i>=0; i--)
{
let x = 0;
while(change >= currencies[i] && cid[i][1] > 0)
{
x += currencies[i];
change -= currencies[i];
cid[i][1] -= currencies[i];
change = Math.round(change*100)/100;
}
if(x > 0)
{
changeArr.push([cid[i][0], x]);
}
}
return {
status : "OPEN",
change: changeArr
}
}
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: