Hi! I wrote a code for JS challenge #5(Cash Register) and can not pass the third test. It works tho with some other numbers, if I change “cash” to 200 for example it returns everithing properly. Been sitting with this for several days already. Asking for help.
function checkCashRegister(price, cash, cid) {
var change11 = cash - price; //MONEY TO GIVE TO CUSTOMER
change11 = change11.toFixed(2);
console.log(change11);
var total = 0;
for(let z = 0; z < cid.length;z++){
total += cid[z][1];
}
total = total.toFixed(2);
console.log(total);
var amount = 0;
var array = [];
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
}
var customersMoney = {
"status": "1",
"change": "array"
}
// ::status INSUFFICIENT_FUNDS::
if(total<change11){
customersMoney["status"] = "INSUFFICIENT_FUNDS";
customersMoney["change"] = [];
}
// ::status CLOSED::
else if(total==change11){
customersMoney["status"] ="CLOSED";
customersMoney["change"] = cid;
}
// ::status OPEN::
else if(total>change11){
for(let i = cid.length-1; i>=0; i--){
var check = cid[i][0];
//console.log(check);
if(cid[i][1]==0) continue;
else if(change11 == 0) break;
else if(change11<obj[check]) continue;
else if(cid[i][1]>0&&change11>=obj[check]){
amount = 0;
while(cid[i][1]>0&&change11>=obj[check]){
if(change11<obj[check]) break;
//else if(cid[0][1]==0) break;
else{
change11 = change11 - obj[check]; //1.74 0.74 0.49 0.24
cid[i][1] = cid[i][1] - obj[check]; //50 89 4.00 3.75
change11 = Math.round(change11 * 100) / 100;
amount = amount + obj[check]; //5 6 6.25 6.50
amount = Math.round(amount * 100) / 100;
}
}
array.push([check,amount]);
}
}
if(change11>0){ //IF THERE IS STILL CHANGE DUE -> NO ENOUGH MONEY
customersMoney["status"] = "INSUFFICIENT_FUNDS";
customersMoney["change"] = [];
}
else if (change11==0){ //IF CHANGE == 0 -> OK
customersMoney["status"]="OPEN";
customersMoney["change"]=array;
}
}
console.log(amount);
console.log(customersMoney["change"]);
return customersMoney;
}
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]]);