Can someone please tell me why my while loop exits before the condition is false and goes to if condition?

It looks from the output that the while loop exits before it completes all iterations and starts printing from the if conditional. I’m going crazy trying to figure out why
It looks like its messing up my program. I think I’m so close like a few statements away

  **Your code so far**

function checkCashRegister(price, cash, cid) {
let moneyAtHand = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]];

let moneyDeno = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];

let drawer = { status: "", change: [] };
var change = cash - price;
let changeArr = []; //["TWENTY", 60]
let cashBackNum = 0;
let moneyinDrawer = cid.reduce((sum, a) => sum + a[1], 0);
if (change <= moneyinDrawer) {
  for (let i = moneyAtHand.length - 1; i >= 0; i--) {
    cashBackNum = 0;
    if (moneyDeno[i] <= change && moneyAtHand[i][1]>=moneyDeno[i]) {
      changeArr.push(moneyAtHand[i]); //["TWENTY", 60]
      console.log(changeArr);

    }
    while (moneyDeno[i] <= change && moneyAtHand[i][1]>=moneyDeno[i]) { 
      console.log("..................")
      console.log("Money Deno "+moneyDeno[i]);
      console.log("Change "+change);
      console.log("Money at hand "+moneyAtHand[i][1]);
      console.log("Cash back "+cashBackNum);
      console.log(moneyAtHand[i]);
      console.log("_______________________");
     
      cashBackNum += moneyDeno[i]; 
      change -= moneyDeno[i]; 
       
      changeArr[changeArr.indexOf(moneyAtHand[i])][1] = cashBackNum;
      moneyAtHand[i][1] -= moneyDeno[i]; //60
      
    }
  }
  drawer.status = "OPEN";
  drawer.change = changeArr;
//  console.log(drawer)
}
else {
  drawer.status = "INSUFFICIENT_FUNDS"
  drawer.change = [];
}
return drawer;
}

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.177

Challenge: Cash Register

Link to the challenge:

@DanCouper @twotani @JeremyLT @jwilkins.oboe guys will you please take a look. I wanted to finish it today very much. I think it’s because of it that is code going from while loop to if statement that my Money at hand is going from 60 to 20 instead of going from 60 to 40

I don’t see the case for “CLOSED”. The case for “INSUFFICIENT FUNDS” could happen in two places: total cash is less than change and change cannot be made by the denominations in cash register (this happens when you go through the change routine but at the end, the change is still greater than 0. e.g. need to return 3 pennies, but no pennies in the register). I will write the control in this way (handling the simpler case first):

if (totalCash < change) {
   //in sufficient fund
} else if (totalCash == change) {
  //exact amount; close the business, return what's in the register
} else { 
  //try to make change
  if (change > 0) {
    //change cannot be made; insufficient fund
  } else {
   //return change; business is still open
  }
}

Lastly, I change the denominations into cents so I deal strictly with integers. Floating point numbers are imprecise so it could cause a problem when trying to compute the amount of change.

Yes I haven’t gotten there yet and thought it would be a short fix if I could get this main case right. For now I was trying to figure out why my while loop isn’t looping that is it goes through the first loop and in the second loop even though the both the conditions match, it isn’t entering the loop.

I wonder why the moneyAtHand reduces to 20 from 60 after this line @twotani

changeArr[changeArr.indexOf(moneyAtHand[i])][1] = cashBackNum;
 console.log("Money at hand "+moneyAtHand[i][1]);

Nevermind I get this part. It was because I was referencing to the array itself when I was pushing it. Will never forget how array references now. Rest of the challenge now remaining.

I suggest you to simplify the for loop body. Instead of adding moneyAtHand[I] to changeArr and then update the amount of that denomination in the while loop, compute that amount first and then add the denomination and amount at the end. This way you don’t have to mess with updating moneyAtHand[I].

This is a general idea.

for (let i = moneyAtHand.length - 1; i >= 0; i--) {
  if (you can use this denom[i]) {
     cnt = how many you need
    amt = actual amount to include fo this denomination
     changeArr.push({denomination, amt})
     change = change - amt
   }
}
1 Like

I will change later for now happy that I completed it after a heavy almost 12 hours

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.