Tell us what’s happening:
I’ve been banging my head against this one for a while now. I have coded what appears to be a satisfactory solution for counting down change, and it works in all cases except those where it needs pennies, in which case I always get an Off By One error for some reason that doesn’t affect the other denominations. Also in cases where there are many pennies I get a rounding error, but I don’t think that’s related as the trailing zeros don’t show up for small amounts.
I’ve tried changing many parts of the loops in the code to get it to work, but nothing seems to help. I’ve been at this for a few hours now so I’m going to bed, hopefully to solve it in the morning, but I thought I might as well post it so maybe a more experienced pair of eyes might spot what I don’t see.
I haven’t touched the part of the challenge that requires me to set the status, as I want to solve the change part of the challenge first, so I have the status set to “OPEN” just so when I run the test I can see if it passes.
Thanks in advance for any help! I feel like it’s got to be something that will be obvious tomorrow but I can’t allow it to keep me up any longer than this.
Your code so far
function checkCashRegister(price, cash, cid) {
//set up variables
let chk = [100, 20, 10, 5, 1, .25, .1, .05, .01]
let owe = cash - price;
let pos = 0;
let ret = {status: "OPEN", change: []}
//begin loop until change owed is 0
for (let i = 0; owe > 0; i++) {
for (let j = 0; j < chk.length; j++) {
//set an easy reference for the position of the bill/coin in the cid array
let pos = (cid.length - 1 - j)
//make sure the loop doesn't end earlier than the change runs out
for (let k = 0; cid[pos][1] > 0 && owe >= chk[j]; k++) {
//if what is owed is greater than the bill/coin value, subtract it
if (owe > 0) {
//if the change array is empty, push this as the first value
if (ret.change.length == 0) {
ret.change.push([cid[pos][0], chk[j]]);
cid[pos][1] -= chk[j];
owe -= chk[j]
}
//if this value doesn't exist in the change array, push it in;
else if (ret.change[ret.change.length - 1][0] != cid[pos][0]) {
ret.change.push([cid[pos][0], chk[j]]);
cid[pos][1] -= chk[j];
owe -= chk[j]
}
//if a spot already exists, simply add to the value in change
else {
ret.change[ret.change.length - 1][1] += chk[j];
cid[pos][1] -= chk[j];
owe -= chk[j]
}
}
}
}
}
return ret;
}
console.log(JSON.stringify(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/72.0.3626.109 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register