Tell us what’s happening:
i have returned the answer in the console and it shows the right answer. However if i try it on freecodecamp, it does not pass.
Warning-- for the third test. this code will go into an infinite loop. I don’t know why just yet.
the main problem-- Why is it not passing the second test on the cash register challenge
Your code so far
function checkCashRegister(price, cash, cid) {
var cashAvailable = 0;
var changeOwed = cash-price;
var change = {
'ONE HUNDRED':100,
'TWENTY':20,
'TEN':10,
'FIVE':5,
'DOLLAR':1,
'QUARTER':0.25,
'DIME':0.1,
'NICKEL':0.05,
'PENNY':0.01
}
var yourChange = {
status:"OPEN",
change: []
}
var noFunds = {
status:"INSUFFICIENT_FUNDS",
change: []
};
var closed = {
status:"CLOSED",
change:cid
};
var flatt = cid.reduce(
function(acc, curr){
return acc.concat(curr);
})
for(var i = 0; i<flatt.length; i++){
if(typeof flatt[i] === "number"){
cashAvailable += flatt[i];
}
}
cashAvailable = Math.round(100*cashAvailable)/100
if(cashAvailable < changeOwed){
return noFunds;
}else if(cashAvailable === changeOwed){
return closed;
}
//the below code is for coins only and changeOwed is less than one dollar.
var coins = 0;
for(var i = 3; i>=0;i--){
if(cid[i][1] > 0.00){
coins += cid[i][1];
}
}
if(changeOwed > coins && changeOwed < 1.00){
return noFunds;
};
//the below code if for the actual change returned
var ans = [];
var compiler = 0;
for(var prop in change){
var check = changeOwed - change[prop];
if( check < changeOwed && check > 0){
for(var i = cid.length -1; i > -1;i--){
if(cid[i][0]=== prop){
var changeAvailable = cid[i][1]
while(changeAvailable !== 0 && changeOwed !== 0){
changeAvailable = changeOwed - change[prop];
changeOwed = changeOwed - change[prop];
compiler +=change[prop];
if (changeAvailable === 0 || changeOwed <= 0 ){
ans.push([prop,compiler]);
}
}
yourChange.change.push(ans);
}
}
}
}
return yourChange;
}
checkCashRegister(19.5, 20, [["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_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/