Tell us what’s happening:
My code seems to work and gets the appropriate results, but it does not pass the tests. I’ll take any help given. Thanks, Y’all are awesome.
Your code so far
function checkCashRegister(price, cash, cid) {
let money = {'PENNY': 0.01, 'NICKEL': 0.05, 'DIME': 0.1, 'QUARTER': 0.25, 'ONE': 1, 'FIVE': 5, 'TEN': 10, 'TWENTY': 20, 'FIFTY': 50, 'ONE HUNDRED': 100}
let total = cid.map((x ) => x[1]).reduce((a, b) => a+b).toFixed(2);
let quantity = cid.map((x) => Math.ceil(x[1] / money[x[0]]));
let chan = cash - price
let change = chan.toFixed(2);
let changes = chan.toFixed(2);
let length = quantity.length -1;
let changeArr = [];
let status = "";
function changer(change, len) {
if (change > 0 && len < 0) {
status = "INSUFFICIENT_FUNDS"
changeArr = []
} else if (len < 0 && total == changes){
status = "CLOSED"
} else if (len < 0){
console.log('paid', total)
status = "OPEN"
} else {
let bills = change / money[cid[len][0]]
let draw = quantity[len]
if (bills >= 1 && draw >=1) {
if (draw > bills) {
let given = money[cid[len][0]] * Math.floor(bills)
let changeLeft = change -= given;
console.log([cid[len][0], given.toFixed(2), changeLeft.toFixed(2), money[cid[len][0]], Math.round(bills) ])
changeArr.push([cid[len][0], given.toFixed(2)])
len--
changer(changeLeft.toFixed(2), len)
} else {
let given = money[cid[len][0]] * draw
let changeLeft = change -= given;
console.log([cid[len][0], given.toFixed(2), changeLeft.toFixed(2)])
changeArr.push([cid[len][0], given.toFixed(2)])
change -= given;
len--
changer(changeLeft.toFixed(2), len)
}
} else {
len--
changer(change, len)
}
}
}
changer(change, length);
return {status: status, change: changeArr}
}
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/67.0.3396.99 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register