My code is shown down below, for some reason, it passed all the tests except for this one. It doesn’t wanna add the last penny for some odd reason… (as in finalArr keeps returning [[“PENNY”, 0.49]]) with 0.01 left in the till.
var valArr = [["ONE HUNDRED", 100.00], ["TWENTY", 20.00], ["TEN", 10.00], ["FIVE", 5.00], ["ONE", 1.00], ["QUARTER", 0.25], ["DIME", 0.10], ["NICKEL", 0.05], ["PENNY", 0.01]];
function checkCashRegister(price, cash, cid) {
cid = cid.reverse();
var change = cash - price;
var total = cid.reduce(function(a,b){
return a + b[1];
}, 0.0).toFixed(2); //intital value of a is 0.0
if(total === change){
return 'Closed';
}
else if (total < change){
return "Insufficient Funds";
}
var finalArr = valArr.reduce(function(acc, currVal, currInd){
if (change >= currVal[1] && cid[currInd][1] !==0){
var x = 0.00;
while (change >= currVal[1] && cid[currInd][1] >= currVal[1]){
x += currVal[1];
change -= currVal[1];
change = Math.round(change*100)/100; //weird JS math
cid[currInd][1] -= currVal[1];
}
x = Math.round(x*100)/100;
acc.push([cid[currInd][0], x]);
return acc;
}
else{
return acc;
}
},[]);
var postTotal = cid.reduce(function(a,b){
return a + b[1];
}, 0.0).toFixed(2);
if(postTotal === 0){
return "Closed";
}
// means if true return this, otherwise return that
return finalArr.length > 0 && change == 0 ? finalArr : postTotal === 0 ? "Closed" : "Insufficient Funds";
}
checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);```
**Your browser information:**
Your Browser User Agent is: ```Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36```.
**Link to the challenge:**
https://www.freecodecamp.org/challenges/exact-change