Issue with for loop in Cash Register

Issue with for loop in Cash Register
0

#1

Tell us what’s happening:
I’m having issues with my loop. I’ve tried with both a while and a for, but both loop through once, and then quit. I’ve tried counting up, and counting down, and I’ve tried in different editors.
Any ideas?
Thanks!
Your code so far


function checkCashRegister(price, cash, cid) {
  var change;
  // Here is your change, ma'am.
  var currency = {
    PENNY: 0.01,
    NICKEL: 0.05,
    DIME: 0.1,
    QUARTER: 0.25,
    ONE: 1,
    FIVE: 5,
    TEN: 10,
    TWENTY: 20,
    "ONE HUNDRED": 100
  };
  function getChange(amount, cid) {
    var oldCid = Object.assign({}, cid);
    var out = [];
    for (var i = 0; i < cid.length; i++) {
      cid[i][1] /= currency[cid[i][0]];
      cid[i][1] = Math.round(cid[i][1]);
    }
    //alert(cid)
    
    for (var j = 0; j < cid.length; j++) {
      alert(j)
      //i = (cid.length-1)-j
      //alert("i is " + i)
      /*if (
        Math.floor(amount / currency[cid[i][0]]) > 0 &&
        Math.floor(amount / currency[cid[i][0]]) < oldCid[i][1]
      ) {
        amount -= Math.floor(amount / currency[cid[i][0]]);
        out.push([cid[i][0], Math.floor(amount / currency[cid[i][0]])]);
      }*/
    }
    
    if (amount <= 0) {
      //alert(out);
      return out;
    }
    return false;
  }
  var total = 0;
  for (var i = 0; i < cid.length; i++) {
    total += cid[i][1];
  }
  if (cash - price > total) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }
  if (cash - price === total) {
    return { status: "CLOSED", change: cid };
  } else {
    var money = getChange(cash-price, cid);
    alert(money);
    if (money) {
      return { status: "OPEN", change: money };
    }
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }
  return change;
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
//alert(2.05/0.05)
checkCashRegister(19.5, 20, [
  ["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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register


#2

So do you know what cid is? Do you know its length? Add a console.log for cid and its length and you willknow for sure why your loop only goes through once.


#3

cid.length is 9; I’ve already checked that.
Any other ideas?


#4

I stand by my initial suggestion…


#5

Cid is

[["PENNY", 101],
["NICKEl" 41],
["DIME", 31],
etc

It’s length is 9.
I’ve added alerts to check for both.
Any other ideas?


#6

checkCashRegister(19.5, 20, [
[“PENNY”, 1.01],
[“NICKEL”, 2.05],
[“DIME”, 3.1],
[“QUARTER”, 4.25],
[“ONE”, 90],
[“FIVE”, 55],
[“TEN”, 20],
[“TWENTY”, 60],
[“ONE HUNDRED”, 100]
]);

What do you see as being passed in as the third param? I see an array of arrays. Therefore its length is 1.


#7

(Actually, I’ve changed CID by this point so that it contains the numbers of each denomination, not the cash amount)
An array of arrays’ length is the number of arrays it contains. [[],[],[]] would have length three.


#8

The 3rd argument being passed into the checkCashRegister function is an array with 9 sub-arrays. It has a length of 9 not 1.


#9

Thanks.
Do you have any ideas why the loop is malfunctioning?


#10

@Aviage-01 I plugged the code you have at top and the loop iterates 9 times. Doesn’t pass 2 of the tests, so it’s an issue of code logic now.


#11

Did you get a series of alerts saying ‘8’, ‘7’, ‘6’, etc?
Because it’s still not looping for me.


#12

I don’t like using alerts, I prefer console.log(). Console.log() shows me that the loop seems to be iterating correctly.


#13

Well, I don’t know what the problem was, but removing all the alerts seemed to fix it. Thanks!!!


#14

Hi, just wanted to apologize for the misleading statements above…


#15

No harm done!
Although I hate to admit it, I regularly make similar mistakes.