I pass all but the final condition:
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}
// algorithm below - please help me deal with my rounding error to pass this final condition test
function checkCashRegister(price, cash, cid) {
let changeObject =
{"ONE-HUNDRED": 0,
"TWENTY": 0,
"TEN": 0,
"FIVE": 0,
"ONE": 0,
"QUARTER": 0,
"DIME": 0,
"NICKEL": 0,
"PENNY": 0}
var box = [];
let balance = cash - price;
console.log(balance); //
let drawerArray = cid.map((elem) => elem[1]);
console.log(drawerArray);
let typeOfMoney = [.01,.05,.10,.25,1,5,10,20,100]
function giveMoney() {
//let typeOfMoney = [.01,.05,.1,.25,1,5,10,20,100] // use "delete" to solve
// this kills the function and returns the final change object
if (balance < 0.01) {
for (var prop in changeObject)
{
if(changeObject[prop] != 0){ box.push([prop,changeObject[prop]])}
}
let status1 = "OPEN";
if (drawerArray[0] < .001) {
status1 = "CLOSED";
};
let answerObj = {};
answerObj["status"] = status1;
answerObj["change"] = box
console.log(drawerArray);
console.log(box);
return answerObj;
}
// --------------------------------------------------------------
if (balance > 0.001 && typeOfMoney.length === 0) {
let status1 = "INSUFFICIENT_FUNDS";
var answerObj = {};
answerObj["status"] = "INSUFFICIENT_FUNDS" ;
answerObj["change"] = [];
return answerObj;
}
// this removes one unit of type of money if balance is below that unit value
if (balance > 0 && balance < typeOfMoney[typeOfMoney.length - 1]) {
//console.log(balance);
//console.log(typeOfMoney);
typeOfMoney.pop();
drawerArray.pop()
console.log(typeOfMoney);
console.log(drawerArray);
console.log(balance)
return giveMoney()
}
//-----------------------------------------------------------------------
//This says what to do if you don't have enough big bills
if (balance >= typeOfMoney[typeOfMoney.length - 1] && drawerArray[drawerArray.length -1] < typeOfMoney[typeOfMoney.length - 1]) {
drawerArray.pop()
typeOfMoney.pop();
return giveMoney()
}
//-------------------------------------------------------------------------
if (balance >= typeOfMoney[typeOfMoney.length - 1] && drawerArray[drawerArray.length -1] >= typeOfMoney[typeOfMoney.length - 1])
// ^if change due is more than(or =) type of money and more than (or =) what you hold of that type
{ drawerArray[drawerArray.length -1] -= typeOfMoney[typeOfMoney.length - 1];
// ^subtract one unit of type of money from drawer
balance -= Math.round(typeOfMoney[typeOfMoney.length - 1]*100)/100;
balance = Math.round(balance * 100) / 100;
console.log(balance);
console.log(changeObject);
//^ subtract one unit of type of money from balance
//console.log(balance + "check");
switch(typeOfMoney[typeOfMoney.length -1]){
case 100:
changeObject["ONE-HUNDRED"] += 100;
break;
case 20:
changeObject["TWENTY"] += 20;
break;
case 10:
changeObject["TEN"] += 10;
break;
case 5:
changeObject["FIVE"] += 5;
break;
case 1:
changeObject["ONE"] += 1;
break;
case .25:
changeObject["QUARTER"] += .25;
break;
case .10:
changeObject["DIME"] += .10;
break;
case .05:
changeObject["NICKEL"] += .05;
break;
case .01:
changeObject["PENNY"] += Math.round(.01*100)/100;
break;
//^add one unit of type of money to change given(changeObject);
}
console.log(balance);
return giveMoney();
}
}
return giveMoney();
}
checkCashRegister(19.9, 20, [["PENNY", 0.1], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);