This is my first thread/comment here, so hello everybody :),
I’m currently working on the Cash Register challenge but got stuck really badly. Hope you guys can help me out. First of all here is my code:
function checkCashRegister(price, cash, cid) {
let change = parseFloat((Number(cash - price).toFixed(2)));
let outputChange = [];
var moneyInDrawer = 0;
var copyCid = JSON.parse(JSON.stringify(cid));
// Creating object for the output
result = {
status : "",
change : []
}
//Summing up the money in the drawer
for (let x = 0; x < copyCid.length; x++) {
moneyInDrawer += copyCid[x][1];
}
moneyInDrawer = Number(moneyInDrawer.toFixed(2));
//Map to compare value
const unitAmountMap = {
"PENNY" : 0.01,
"NICKEL" : 0.05,
"DIME" : 0.1,
"QUARTER" : 0.25,
"ONE" : 1.00,
"FIVE" : 5.00,
"TEN" : 10.00,
"TWENTY" : 20.00,
"ONE HUNDRED" : 100.00
}
for (i = 8; i >= 0; i--) {
var helperArr = []; //Subarray ["UNIT", amount]
var amount = 0;
helperArr.push(copyCid[i][0]); //Adding UNIT to subarray
while (change - unitAmountMap[copyCid[i][0]] >= 0 && copyCid[i][1]) { //Calculating amount per unit
change = parseFloat(Number(change - unitAmountMap[copyCid[i][0]]).toFixed(2));
amount = parseFloat(Number(amount + unitAmountMap[copyCid[i][0]]).toFixed(2));
moneyInDrawer = parseFloat(Number(moneyInDrawer - unitAmountMap[copyCid[i][0]]).toFixed(2));
copyCid[i][1] = parseFloat(Number(copyCid[i][1] - unitAmountMap[copyCid[i][0]]).toFixed(2));
}
helperArr.push(amount);
if (helperArr[1] > 0) {
outputChange.push(helperArr);
}
}
if (change > 0) {
result.status = "INSUFFICIENT_FUNDS";
return result;
}
else if (change == 0 && moneyInDrawer == 0) {
result.status = "CLOSED";
result.change = cid;
return result;
}
else if (change == 0 && moneyInDrawer > 0) {
result.status = "OPEN";
result.change = outputChange;
console.log(typeof(result))
return result;
}
}
I ran all the test cases in the dev mode in my browser and think the outputs are correct. Anyway, none of the tests passes. After debugging and browsing through the forum for hours I am quite desperate
Obviously, I am not delivering the required object, although typeof(result)
shows the correct return type.
I’m very grateful for your answers.
Gabriel