Tell us what’s happening:
I believe the code is working well but the fifth case and the sixth case are popping errors. When one analyses the expected results they seem to be erroneous. The fifth, for example, does have enough funds but not the correct currency( it has 1 penny and 1 dollar). Something similar happens in the sixth test. Please help.
**Your code so far**
function checkCashRegister(price, cash, cid) {
let changeX = {status:'', change:[]};
// currrency X 100
let currency = [
["PENNY", 1],
["NICKEL", 5],
["DIME", 10],
["QUARTER", 25],
["ONE", 100],
["FIVE", 500],
["TEN", 2000],
["TWENTY", 6000],
["ONE HUNDRED", 10000]
]
//check if there is money in the cash register
let money = 0;
for(let i = 0 ; i < cid.length; i++)
{
money += cid[i][1];
}
//console.log(money.toFixed(2));
let back = (cash - price).toFixed(2);
if (back > money)
{
changeX.status = "INSUFFICIENT_FUNDS";
return changeX;
}
//return the propper amount - multiply X 100 to avoid rounding errors. Hate float numbers
let current = back*100;
let valuesUsed = 0;
for(let i = cid.length - 1; i >= 0; i--)
{
//check if there is money
if (cid[i][1] > 0)
{
if(current % currency[i][1] < current)
{
//Use as much as you can of that highest currency and determine how much you can use
let cur = Math.round(cid[i][1] / (currency[i][1] / 100));
let min = Math.floor(current / currency[i][1]);
changeX.change.push(currency[i]);
//if the amout needed is lower than what is there then procede otherise use all there is
if (min <= cur)
{
//console.log(cur + ' - current -> ' + current + ' min ' + min + ' ---- ' + i + ' --c-- ' + currency[i][1]);
current -= (min * currency[i][1]);
changeX.change[valuesUsed][1] = ((min * (currency[i][1]/100)).toFixed(2))*1;
//multiply by 1 to avoid trailing zeros
valuesUsed ++;
}
else
{
current -= (cur * currency[i][1]);
changeX.change[valuesUsed][1] = ((cur * (currency[i][1]/100)).toFixed(2))*1;
valuesUsed ++;
}
}
}
}
if (current === 0)
{
changeX.status = "OPEN";
}
else
{
changeX.status = "CLOSED";
}
console.log(changeX);
return changeX;
}
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0
.
Challenge: Cash Register
Link to the challenge: