Tell us what’s happening:
it has an unexpected result for
CLOSED condition
I am not able to find this but perhaps this is due to last index array element of 0
please check
Your code so far
function checkCashRegister(price, cash, cid) {
var change = cash - price;
var result2 ;
var arr = [];
let total1 =0
var obj = {
'PENNY':0.01,
'NICKEL':0.05,
'DIME':0.1,
'QUARTER':0.25,
'ONE':1,
'FIVE': 5,
'TEN':10,
'TWENTY':20,
'ONE HUNDRED': 100
}
let l = cid.length-1 ;
for(let i=l; i>=0; i--){
total1 += cid[i][1] ;
console.log(i)
if (change > total1){
result2={'status':'INSUFFICIENT_FUNDS','change':[]} ;
break ;
}
else if (change === total1){
result2 = {'status':"CLOSED",'change':cid};
break ;
}
else if (change > obj[cid[i][0]]) {
if (change > cid[i][1]){
change = (change % cid[i][1]).toFixed(2);
arr.push(cid[i])
// continue ;
} else {
let num = parseInt(change / obj[cid[i][0]])*obj[cid[i][0]]
change = (change % obj[cid[i][0]]).toFixed(2)
cid[i][1] = num ;
arr.push(cid[i]);
continue ;
}
}
}
if ((cash-price) < total1)
result2 = {status:"OPEN",change:arr}
// console.log(cid)
return result2 ;
}
console.log()
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]))
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: