I’ve been experiencing a strange issue while trying to debug my Cash Register challenge.
Inside of a “while” loop that needs to go 50 times (it needs to collect 50 pennies for the final test case), if I add a simple console.log(“hello”); inside the loop, it will stop after something like 35 iterations. If I add a second log statement, it will stop after about 30 iterations.
I initially thought that maybe somehow by accessing the variables I was logging, I was changing their values and affecting the “while” test, but then found that even if I just log plain strings like “hello” the loop quits early.
Has anyone experienced anything like this? I’ve also tried reloading the page, pasting in my code and trying it again. It would work properly with log statements for 1 or 2 runs, and then start exhibiting the weird behavior again.
Here’s my code, you’ll see some log statements to uncomment within the while loop:
function checkCashRegister(price, cash, cid) {
var changeRemaining = roundTwo(cash - price); //the amount of change left to give
let status = "";
var change = []; //will store the change array to be returned for OPEN case
let drawer = []; //will store a copy of cid that we can modify
//copy drawer's sub-elements so we don't modify the original cid array
for (let i = 0; i < cid.length; i++) {
drawer.push([...cid[i]]);
}
//to find the value of the various bills/coins
let billsAndCoins = {
"ONE HUNDRED": 100,
"TWENTY": 20,
"TEN": 10,
"FIVE": 5,
"ONE": 1,
"QUARTER": 0.25,
"DIME": 0.1,
"NICKEL": 0.05,
"PENNY": 0.01
}
//found that I had to keep rounding the numbers back to 2 decimals
function roundTwo(num) {
//return Number(num.toFixed(2));
return Math.round(num * 100) / 100;
}
//Go through all bills/coins in descending order
for (let i = drawer.length-1; i >= 0; i--) {
let billSize = roundTwo(billsAndCoins[drawer[i][0]]);
//console.log("billSize, changeRemaining: " + billSize + ", " + changeRemaining);
if (billSize <= changeRemaining) {
//collect pile of those bills/coins
let pile = [drawer[i][0], 0];
while ((changeRemaining >= billSize) && (drawer[i][1] > 0)) {
changeRemaining = roundTwo(changeRemaining - billSize);
pile[1] = roundTwo(pile[1] + billSize);
drawer[i][1] = roundTwo(drawer[i][1] - billSize);
//console.log("UNCOMMENT ME");
//console.log("UNCOMMENT ME TOO " + changeRemaining);
}
if (pile[1] > 0) {
change.push(pile);
}
}
}
let drawerRemaining = roundTwo(drawer.reduce((a, b) => {
return a + b[1];
}, 0));
console.log("final changeRemaining (should be 0): " + changeRemaining);
console.log("drawerRemaining (should be 0): " + drawerRemaining);
if (changeRemaining > 0) {
status = "INSUFFICIENT_FUNDS";
change = [];
}
if (changeRemaining == 0 && drawerRemaining == 0) {
status = "CLOSED";
change = cid;
}
if (changeRemaining == 0 && drawerRemaining > 0) {
status = "OPEN";
}
return {status: status, change: change};
}
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));
Thanks in advance!