Exact Change - for loop issues

Exact Change - for loop issues
0

#1

Tell us what’s happening:

For some reason my for loop that iterates backwards through cid seems to run several times. I’ve used a lot of console.log() commands to keep track of certain values. When I look at the console after running this, I see i get to 0, but the log is showing that i resets itself to 8 several times and executing the loop more than once.

Your code so far

function checkCashRegister(price, cash, cid) {
  var amounts = {"PENNY": 0.01,
                 "NICKEL": 0.05,
                 "DIME": 0.10,
                 "QUARTER": 0.25,
                 "ONE": 1,
                 "FIVE": 5,
                 "TEN": 10,
                 "TWENTY": 20,
                 "ONE HUNDRED": 100};
  var change = 0;
  var cidAmount = 0;
  var changeDue = cash - price;
  var changeCid = [];
  
  cid.forEach(function(item){
    cidAmount += item[1];
  });
  
  cidAmount = Math.round(cidAmount * 100) / 100;
  
  if(cidAmount == changeDue){
    return "Closed";
  }
  else if(cidAmount < changeDue){
    return "Insufficient Funds";
  }
  console.log(cid.length);
  for(var i = cid.length-1; i > 0; i--){
    console.log(i + " = i");
    var bill = cid[i][1];//amount of current bill in drawer
    console.log(bill + ": Bill");
    var amount = amounts[cid[i][0]]; //denomination of bill
    console.log(amount + ": amount");
    var numOfBill = Math.floor(changeDue/amount); //number of bills to give
    if(numOfBill * amount > bill){
      numOfBill = bill/amount;
    }
    console.log(changeDue);
    console.log(numOfBill+ ": num");
    
    if(numOfBill > 0 && changeDue !== 0){
      changeCid.push(cid[i][0], amount * numOfBill);
      changeDue -= amount * numOfBill;
    }
    
  }
  
  if(changeDue < 0.01){
    return changeCid;
  }
  
  return "Insufficient Funds";
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

When I run your code with the following call, I only see i = 8 one time as it should based on the for loop design.

checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

Below is what I see in the console:

9
8 = i
100: Bill
100: amount
96.74
0: num
7 = i
60: Bill
20: amount
96.74
3: num
6 = i
20: Bill
10: amount
36.739999999999995
2: num
5 = i
55: Bill
5: amount
16.739999999999995
3: num
4 = i
90: Bill
1: amount
1.7399999999999949
1: num
3 = i
4.25: Bill
0.25: amount
0.7399999999999949
2: num
2 = i
3.1: Bill
0.1: amount
0.23999999999999488
2: num
1 = i
2.05: Bill
0.05: amount
0.03999999999999487
0: num
=> ‘Insufficient Funds’

If you look at the browser’s console after running the FCC tests, the you will see i get reset to 8 as many times as there are tests, because the tests are ran consecutively.


#3

That’s strange, do you mind if I ask what browser you are using?


#4

Latest version of Chrome on Windows 8.1


#5

Hmmm, must be a freecodecamp bug. Because when I run this (just once) in the challenge I get this in my console. I’ll probably just have to refactor my code and find a different way of doing it.

9
VM70:34 8 = i
VM70:36 100: Bill
VM70:38 100: amount
VM70:43 96.74
VM70:44 0: num
VM70:34 7 = i
VM70:36 60: Bill
VM70:38 20: amount
VM70:43 96.74
VM70:44 3: num
VM70:34 6 = i
VM70:36 20: Bill
VM70:38 10: amount
VM70:43 36.739999999999995
VM70:44 2: num
VM70:34 5 = i
VM70:36 55: Bill
VM70:38 5: amount
VM70:43 16.739999999999995
VM70:44 3: num
VM70:34 4 = i
VM70:36 90: Bill
VM70:38 1: amount
VM70:43 1.7399999999999949
VM70:44 1: num
VM70:34 3 = i
VM70:36 4.25: Bill
VM70:38 0.25: amount
VM70:43 0.7399999999999949
VM70:44 2: num
VM70:34 2 = i
VM70:36 3.1: Bill
VM70:38 0.1: amount
VM70:43 0.23999999999999488
VM70:44 2: num
VM70:34 1 = i
VM70:36 2.05: Bill
VM70:38 0.05: amount
VM70:43 0.03999999999999487
VM70:44 0: num
VM72:30 9
VM72:34 8 = i
VM72:36 100: Bill
VM72:38 100: amount
VM72:43 96.74
VM72:44 0: num
VM72:34 7 = i
VM72:36 60: Bill
VM72:38 20: amount
VM72:43 96.74
VM72:44 3: num
VM72:34 6 = i
VM72:36 20: Bill
VM72:38 10: amount
VM72:43 36.739999999999995
VM72:44 2: num
VM72:34 5 = i
VM72:36 55: Bill
VM72:38 5: amount
VM72:43 16.739999999999995
VM72:44 3: num
VM72:34 4 = i
VM72:36 90: Bill
VM72:38 1: amount
VM72:43 1.7399999999999949
VM72:44 1: num
VM72:34 3 = i
VM72:36 4.25: Bill
VM72:38 0.25: amount
VM72:43 0.7399999999999949
VM72:44 2: num
VM72:34 2 = i
VM72:36 3.1: Bill
VM72:38 0.1: amount
VM72:43 0.23999999999999488
VM72:44 2: num
VM72:34 1 = i
VM72:36 2.05: Bill
VM72:38 0.05: amount
VM72:43 0.03999999999999487
VM72:44 0: num
VM70:30 9
VM70:34 8 = i
VM70:36 100: Bill
VM70:38 100: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 7 = i
VM70:36 60: Bill
VM70:38 20: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 6 = i
VM70:36 20: Bill
VM70:38 10: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 5 = i
VM70:36 55: Bill
VM70:38 5: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 4 = i
VM70:36 90: Bill
VM70:38 1: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 3 = i
VM70:36 4.25: Bill
VM70:38 0.25: amount
VM70:43 0.5
VM70:44 2: num
VM70:34 2 = i
VM70:36 3.1: Bill
VM70:38 0.1: amount
VM70:43 0
VM70:44 0: num
VM70:34 1 = i
VM70:36 2.05: Bill
VM70:38 0.05: amount
VM70:43 0
VM70:44 0: num
VM70:30 9
VM70:34 8 = i
VM70:36 100: Bill
VM70:38 100: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 7 = i
VM70:36 60: Bill
VM70:38 20: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 6 = i
VM70:36 20: Bill
VM70:38 10: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 5 = i
VM70:36 55: Bill
VM70:38 5: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 4 = i
VM70:36 90: Bill
VM70:38 1: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 3 = i
VM70:36 4.25: Bill
VM70:38 0.25: amount
VM70:43 0.5
VM70:44 2: num
VM70:34 2 = i
VM70:36 3.1: Bill
VM70:38 0.1: amount
VM70:43 0
VM70:44 0: num
VM70:34 1 = i
VM70:36 2.05: Bill
VM70:38 0.05: amount
VM70:43 0
VM70:44 0: num
VM70:30 9
VM70:34 8 = i
VM70:36 100: Bill
VM70:38 100: amount
VM70:43 96.74
VM70:44 0: num
VM70:34 7 = i
VM70:36 60: Bill
VM70:38 20: amount
VM70:43 96.74
VM70:44 3: num
VM70:34 6 = i
VM70:36 20: Bill
VM70:38 10: amount
VM70:43 36.739999999999995
VM70:44 2: num
VM70:34 5 = i
VM70:36 55: Bill
VM70:38 5: amount
VM70:43 16.739999999999995
VM70:44 3: num
VM70:34 4 = i
VM70:36 90: Bill
VM70:38 1: amount
VM70:43 1.7399999999999949
VM70:44 1: num
VM70:34 3 = i
VM70:36 4.25: Bill
VM70:38 0.25: amount
VM70:43 0.7399999999999949
VM70:44 2: num
VM70:34 2 = i
VM70:36 3.1: Bill
VM70:38 0.1: amount
VM70:43 0.23999999999999488
VM70:44 2: num
VM70:34 1 = i
VM70:36 2.05: Bill
VM70:38 0.05: amount
VM70:43 0.03999999999999487
VM70:44 0: num
VM70:30 9
VM70:34 8 = i
VM70:36 0: Bill
VM70:38 100: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 7 = i
VM70:36 0: Bill
VM70:38 20: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 6 = i
VM70:36 0: Bill
VM70:38 10: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 5 = i
VM70:36 0: Bill
VM70:38 5: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 4 = i
VM70:36 1: Bill
VM70:38 1: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 3 = i
VM70:36 0: Bill
VM70:38 0.25: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 2 = i
VM70:36 0: Bill
VM70:38 0.1: amount
VM70:43 0.5
VM70:44 0: num
VM70:34 1 = i
VM70:36 0: Bill
VM70:38 0.05: amount
VM70:43 0.5
VM70:44 0: num


#6

No, it is not a bug. If you look what I said in the last post (see below), you will see the results of what you posted. In my last post, I was talking about when I ran only a single test case.


#7

Ah, I see. Thanks! It must be logging from all of the test cases, not just one.