Tell us what’s happening:
I am having trouble being able to get the right amount of change given the cash in drawer. It seems that my code is able to provide the correct amount of change, but I can’t seem to figure out how to use the CID to allow the function to move on from one unit of money to the next when it is out of stock, or does not have sufficient funds.
Your code so far
function checkCashRegister(price, cash, cid) {
let change = cash-price;
let unit = [['ONE HUNDRED', 0], ['TWENTY', 0] , ['TEN', 0], ['FIVE', 0], ['DOLLAR', 0], ['QUARTER', 0], ['DIME', 0], ['NICKEL', 0], ['PENNY', 0]]
let value = [100, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01]
let cidValues = []
let numbers = []
let sum = 0
let open = []
let closed = []
for (var i = 0; i < cid.length; i++){
sum = sum + cid[i][1]
} // finding sum of cash in drawer
for (var k = cid.length - 1; k >= 0; k--){
cidValues.push(cid[k][1]) // listing the amount of each unit
numbers = cidValues[k]/value[k]
}
for(let j = 0; j < value.length; j++){ // LOOPING THRU TO GET TOTALS
let total = 0
let x = 0
while(change >= value[j]){
change = change - value[j]
total = total + value[j]
x++
if (x === cidValues[j]){
break
}
} if(total[j] > cidValues[j]){
return {status:"INSUFFICIENT_FUNDS", change: []}
}
unit[j][1] = total
} console.log(unit)
if (sum < change){
return {status:"INSUFFICIENT_FUNDS", change: []}
} else if (sum === change){
return {status:"CLOSED", change: unit}
} else if (sum > change){
return {status:"OPEN", change:unit}
}
}
checkCashRegister(19.5, 25, [["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/83.0.4103.61 Safari/537.36.
Challenge: Cash Register
Link to the challenge: