I’m about to go cross-eyed on the Cash Register challenge, so I think it’s time to ask for help. This is where I’m at:
let transaction = {
status: '',
change: [ /*
["ONE HUNDRED", 0],
["TWENTY", 0],
["TEN", 0],
["FIVE", 0],
["ONE", 0],
["QUARTER", 0],
["DIME", 0],
["NICKEL", 0],
["PENNY", 0], */
] };
const currencyValue = [100, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01];
var changeDue = cash - price;
var cidSubTotal = 0;
var cidTotal;
cid.forEach((drawer) => {
cidSubTotal += (drawer[1] * 100);
});
cidTotal = cidSubTotal / 100;
var comparisonDrawer = cid.reverse();
if (changeDue > cidTotal) {
transaction.status = "INSUFFICIENT_FUNDS";
transaction.change = [];
}
if (cidTotal === changeDue) {
transaction.status = "CLOSED";
transaction.change = cid;
}
while (transaction.status === '') {
let index = 0;
/* -Iterate over the currencyValue array, checking
the current index versus the value of the changeDue value.
-If the currencyValue[i] is less than the changeDue AND the corresponding denomination in the comparisonDrawer is positive, perform the following steps:
-Decrement the comparison drawer by the currencyValue amount
-Increment the change array by the currencyValue amount.
-Else the currencyValue is greater than the changeDue, increment the index
so the next smallest denomination can be checked.
*/
if (changeDue === 0 && cidTotal > 0) {
transaction.status = "OPEN";
}
}
console.log(transaction);
return transaction;
}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
There’s obviously a lot of room for improvement here, but I’m concerned with one thing at the current moment: why isn’t this passing in the case of exact change? It was doing so earlier, but I can’t see why it isn’t returning the change array as the cid array, like it’s assigned.