Cash Register issue

Cash Register issue
0

#1

Tell us what’s happening:
Well this is embarrassing. I’m confronted by an error that makes no sense to me. It doesn’t even seem to be an error.
When I (console.) log cid[i] I receive a double log. The first makes sense, as ["ONE HUNDRED", 1], but the second is undefined.
My error in the test output is Cannot read property '0' of undefined.
Any help will be greatly appreciated!

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.length)
    for (var i = cid.length-1; i >= 0; i++) {
      //alert(j)
      //i = (cid.length-1)-j
      //alert("i is " + i)
      console.log("Test")
      if (
        Math.floor(amount / currency[cid[i][0]]) > 0 &&
        Math.floor(amount / currency[cid[i][0]]) < oldCid[i][1]
      ) {
        console.log("Before")
        amount -= Math.floor(amount / currency[cid[i][0]]);
        console.log("After")
        out.push([cid[i][0], Math.floor(amount / currency[cid[i][0]])]);
      }
    }
    console.log("Amount is " + amount)
    if (amount <= 0) {
      console.log(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 that error means you are trying to access an array at index 0 but there is no array there to access.

so if you look at all the lines that you have [0] or something that equals zero, and log the array you think you have, you will get undefined for that array.

give it a try…


#3

I am aware of that error message.
I know that the error message is coming from where I say cid[i][0].
The issue is that cid seems to be being overwritten in the second pass of the for loop.


#4

I feel like a right idiot. I had i++ instead of i–.