Tell us what’s happening:
Im going through the last test i need to satisfy to pass the project.
checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])should return
{status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}
It seems to be getting tripped up at the ten cent(where there is no change to give) instead of looping the the pennies(where there is change to give) to distribute to the change object.
Any help would be greatly appreciated, thanks. =)
**Your code so far**
function checkCashRegister(price, cash, cid) {
// create change object
let change = {
status: "OPEN",
change: []
};
// let change due = cash -price
let changeDue = cash-price;
// Currency values
let priceChart = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
// loop through cid array
for (let x = (priceChart.length - 1); x >= 0; x--){
// loop while changeDue is > the value of the current change denomination
while(changeDue >= priceChart[x]){
/*
**** closed status of money return
*/
// conditional if coin has supply
if(cid[x][1] === 0){
x--;
}
// if the current coin has already been added to change.change
if (currentCoin === cid[x][0]){
// add change on to current coin
change.change[0][1] = change.change[0][1] + priceChart[x];
// reduce amount of bill/coin change from supply
cid[x][1] = cid[x][1] - priceChart[x];
} else {
// add a new array entity for next currency unit
change.change.unshift([cid[x][0], priceChart[x]]);
// reduce amount of bill/coin change from supply
cid[x][1] = cid[x][1] - priceChart[x];
}
// readjust changeDue after current coin has been subtracted
// changeDue = changeDue - priceChart[x]; *****
changeDue = Math.round((changeDue - priceChart[x])*100)/100;
console.log(changeDue);
// return insufficient funds if their is not enough change
if (cid[x][1] < 0) {
change.status = "INSUFFICIENT_FUNDS";
change.change = [];
return change;
}
console.log(change.change + " " + cid[x][1] + " " + changeDue);
// current coin tracker
let currentCoin = cid[x][0];
}
}
// reverse order change.change
change.change = change.change.reverse();
// return change
console.log(change);
return change;
}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["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/95.0.4638.69 Safari/537.36
Challenge: Cash Register
Link to the challenge: