function checkCashRegister(price, cash, cid) {
console.log(cid)
let mainDiff = cash - price;
cid.reverse();
let resultObj = { state: "", change : []};
let lastArr = [];
let totalCash = 0;
let count = 0;
let countMoney = 0;
let x = 0;
cid.forEach(item => {
totalCash += item[1];
});
totalCash = totalCash.toFixed(2);
cid.forEach(item => {
let drawerCount = 0;
let diffCount = 0;
let divNumb = 0;
switch(item[0]){
case "ONE HUNDRED":
divNumb = 100;
drawerCount = item[1]/100;
diffCount = Math.floor(mainDiff/100);
break;
case "TWENTY":
divNumb = 20;
drawerCount = item[1]/20;
diffCount = Math.floor(mainDiff/20);
break;
case "TEN":
divNumb = 10;
drawerCount = item[1]/10;
diffCount = Math.floor(mainDiff/10);
break;
case "FIVE":
divNumb = 5;
drawerCount = item[1]/5;
diffCount = Math.floor(mainDiff/5);
break;
case "ONE":
divNumb = 1;
drawerCount = item[1];
diffCount = Math.floor(mainDiff);
break;
case "QUARTER":
divNumb = 0.25;
drawerCount = Math.round(item[1]/0.25);
diffCount = Math.floor(mainDiff/0.25);
break;
case "DIME":
divNumb = 0.1;
drawerCount = Math.round(item[1]/0.1);
diffCount = Math.floor(mainDiff/0.1);
break;
case "NICKEL":
divNumb = 0.05;
drawerCount = Math.round(item[1]/0.05);
diffCount = Math.floor(mainDiff/0.05);
break;
case "PENNY":
divNumb = 0.01;
drawerCount = Math.round(item[1]/0.01);
diffCount = Math.floor(mainDiff/0.01);
break;
}
if(diffCount > 0 && drawerCount > 0){
if(diffCount >= drawerCount){
x = item[1];
}
else{
x = diffCount * divNumb;
}
}
if(x > 0){
mainDiff -= x;
mainDiff = mainDiff.toFixed(2);
countMoney += x;
lastArr.push([item[0],x]);
x = 0;
}
});
countMoney = countMoney.toFixed(2);
mainDiff = cash - price;
cid.reverse();
if(totalCash > mainDiff){
if(countMoney == mainDiff){
resultObj[status] = "OPEN";
resultObj.change = lastArr;
}
else{
resultObj[status] = "INSUFFICIENT_FUNDS";
}
}
else if(totalCash == mainDiff){
resultObj[status] = "CLOSED";
resultObj.change = cid;
}
else{
resultObj[status] = "INSUFFICIENT_FUNDS";
}
// Here is your change, ma'am.
return resultObj;
}
// 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(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
I’ve forgotten the quotes while setting the value of the status property. Solved