Cash Register Almost Done!

Tell us what’s happening:
I’m on that final challenge for the Javascript section. I wrote a code, and I keep getting a “Cannot read property ‘1’ of undefined” message whenever I try to test it. I’m not sure where the issue is stemming from. Can you find it?

Your code so far

function checkCashRegister(price, cash, cid) {
  let changeRequired = cash - price;
  cid[0].push(.01); cid[1].push(.05); cid[2].push(.10); cid[3].push(.25); cid[4].push(1); cid[5].push(5); cid[6].push(10); cid[7].push(20); cid[8].push(100);
  let drawerTotal = 0;
  for (let i = 0; i <= cid.length; i++) {
     drawerTotal += parseInt(cid[i][1] * 100, 10) / 100;
  let register = {status: "OPEN", change: []}
  if (changeRequired > drawerTotal) {
    register.status = "INSUFFICIENT_FUNDS";
  } else if (changeRequired == drawerTotal) {
    register.status = "CLOSED";
  } else {
    let stageAmount = 0;
    let changeChain = 0;
    let changeSpecs = [];
    while (changeRequired !== 0) {
      for (let j = 0; j < cid.length; j++) {
        stageAmount == changeRequired / cid[j][2]
        if (cid[j][1] / cid[j][2] >= stageAmount) {
          changeSpecs.push([cid[j][0], stageAmount]);
          changeRequired -= stageAmount * cid[j][2]
   } if (changeRequired == 0) {
     register.status = "INSUFFICIENT_FUNDS";
   } else {register.status = "OPEN";
           register.change = changeSpecs; 
  return register

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/72.0.3626.121 Safari/537.36.

Link to the challenge:

cid[length] is undefined, so you cannot access cid[length][1].

I’m somewhat concerned with your while loop. You may be setting yourself up for an infinite loop.

So I removed all references to cid.length, and instead used this:

let newCID = Array.from(cid)

and then I replaced all references to cid to newCID, but still got the same error message.

That’s not what I was getting at. Look at the first loop in your code above.

Not sure I follow.


returns 9. So it appears to have a length property. On the other hand, it clearly doesn’t since I am getting that error. I’m convinced that you are correct that the error stems from here, just still don’t know completely why.

You have a length property. When I said cid[length] I was using shorthand that may have been confusing. It would have been more appropriate to said that cid[cid.length] is undefined. This is referred to as an “index out of bounds error”.

So I can’t have a “for loop” that loops through the index 1 of each array within an array?


returns 1.01 (which is correct). The loop is just iterating through 0 - 8 (the length of cid): cid [ i ] [ 1 ], where “i” would be 0 - 8.

No. The problem is that indexing starts at 0. If an array has a length of 3, then theArray[3] is undefined.

Oh, I see.

for (let i = 0; i <= cid.length; i++) {
     drawerTotal += parseInt(cid[i][1] * 100, 10) / 100;

It should be written at i < cid.length instead of i <= cid.length. I didn’t notice that error.
I can’t believe I didn’t catch that; should have been obvious.

The little things like that get us all. :smiley: Happy coding!