Tell us what’s happening:
Hi everyone, so I’ve been working on the Cash Register Project, and I’ve been having some trouble with the third case.
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]])
should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}
My code stops at the $20 bill mark and decrements it until it is $0. But then it doesn’t iterate it to a $10 bill, and so on. A bit stuck on where to go from this point. Not looking to solve the {insufficient_funds}
challenge case just yet. Any help would be appreciated, thank you for your time,
Your code so far
const currency =
[
["PENNY", 1],
["NICKEL", 5],
["DIME", 10],
["QUARTER", 25],
["ONE", 100],
["FIVE", 500],
["TEN", 1000],
["TWENTY", 2000],
["ONE HUNDRED", 10000]
]
function checkCashRegister(price, cash, cid) {
let changeOwed = Math.round((cash*100) - (price*100));
let currentCID = {};
let changeGiven = {};
console.log(changeOwed)
cid.map(val=> {currentCID[val[0]] = Math.round(val[1]*100)})
let CIDTotal =Object.values(currentCID).reduce(function(a,b){return a+b});
if (changeOwed>CIDTotal) {
return {status: "INSUFFICIENT_FUNDS", change: []}
}
if (CIDTotal===changeOwed ) {
return {status: "CLOSED", change: cid}
}
if (CIDTotal>changeOwed) {
for (let i=currency.length-1; i>=0; i--) {
let name = currency[i][0]
let value = currency[i][1]
while (changeOwed-value > 0) {
changeGiven[name]=0
while (currentCID[name] > 0 && changeOwed - value >= 0 ) {
currentCID[name] -= value
changeGiven[name] += value
changeOwed -= value
}
console.log(changeGiven)
let final = [];
if (changeOwed===0) {
final.push([name, changeGiven[name]/100])
}
return {status: "OPEN", change: final}
}
}
}
}
console.log(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 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: