Tell us what’s happening:
I have doubt about Number.toFixed(), according to my query
total = 335.41
amntDue = 96.74
but when I apply condition
if(amntDue>total){ //It is going to be true but it should not why?
return {status: "INSUFFICIENT_FUNDS", change: []}
}
and if I don’t use Number.toFixed() then the condition goes false …
WHAT THE HACK IS Number.toFixed() is doing with my code…
At last sorry, this is the dangerous code until now, which I have writtern…
Your code so far
function checkCashRegister(price, cash, cid) {
var change = [];
var amntDue = (cash-price).toFixed(2); //-------here the problem is
var check = function (total){
if(total>=amntDue) return true;
else return false;
}
var coinValue = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
]
var total = cid.map(arr=>arr[1]).reduce((a,b)=>a+b).toFixed(2); // --------Here also
if(amntDue == total){
return {status: "CLOSED", change: cid};
}
else if(amntDue>total){
return {status: "INSUFFICIENT_FUNDS", change: []}
}
var i = 0;
while(amntDue > coinValue[i][1]){
i++;
}
for(var j=i-1; j>=0; j--){
if(cid[j][1]>0 && amntDue>coinValue[j][1]){
var need = Math.floor((amntDue/coinValue[j][1]))*coinValue[j][1];
if(need <= cid[j][1]){
change.push([cid[j][0],need]);
amntDue = (amntDue-need).toFixed(2);
total -= need;
}else{
change.push(cid[j]);
amntDue = (amntDue-cid[j][1]).toFixed(2);
total -= cid[j][1];
}
}
}
if(amntDue!=0){
return{status: "INSUFFICIENT_FUNDS", change: []}
}
else;
return {status: "OPEN", change: change};
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
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: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36</code>.
**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/