Cash Register - Help needed!

Tell us what’s happening:

Hi everyone! I’ve been working for quite some time on the last Javascript project “Cash Registry”. I’ve been working on jsfiddle.net. The code seems to work on jsfiddle, but for some reason it doesn’t pass the freecodecamp tests.

Error that is given is the following:
“TypeError: unknown: Cannot read property ‘0’ of undefined”.

Now, I’ve narrowed it down to the count function, but can’t get my head around it.

P.S. my code is a mess, hope it’s not too much trouble!

Cheers!

Your code so far



function checkCashRegister(price, cash, cid) {

  // Counting registry:

  var registry = [];
  var penny = 1;
  var nickel = 5;
  var dime = 10;
  var quarter = 25;
  var one = 100;
  var five = 500;
  var ten = 1000;
  var twenty = 2000;
  var hundred = 10000;

  var money = [penny, nickel, dime, quarter, one, five, ten, twenty, hundred]
  var nimed = ["PENNY", "NICKEL", "DIME", "QUARTER", "ONE", "FIVE", "TEN", "TWENTY", "HUNDRED"];
  for (var i = cid.length - 1; i >= 0; i--) {
    registry.unshift(Math.round(cid[i][1] * 100 / (money[i])));
  }
 // console.log(registry);

  // returning change:
  var kopikad = [];

  var tagasi = (cash - price) * 100;
 // console.log(tagasi);
  var k = 0;


  for (var j = registry.length - 1; j >= 0; j--) {
    while (tagasi >= money[j] && registry[j] != 0) {
      if (kopikad.includes(cid[j][0])) {

      }
      /*else {
           kopikad.push(cid[j][0])
           }*/
      kopikad.push(money[j]);
      tagasi -= money[j];
      registry[j] -= 1;
    }
  }
/*
  console.log(registry);
  console.log(tagasi);
  console.log(kopikad);
*/
  // checking if there is enough change:
  let change = [];

  Object.defineProperties(Array.prototype, {
    count: {
      value: function(query) {
        var count = 0;
        for (let i = 0; i < this.length; i++)
          if (this[i] == query)
            count++;
        return count;
      }
    }
  });
  
  let uus = [];
  for (var l = money.length - 1; l >= 0; l--) {
    let väärtus = 0;
    // console.log(kopikad.count(money[l]));
    väärtus = väärtus + kopikad.count(money[l]) * money[l];
    uus.push(väärtus);
  }
 // console.log(uus);

  var nimed2 = nimed.reverse();
  for (var u = 0; u < uus.length; u++) {
    let uus2 = [];
    if (uus[u] != 0) {
      uus2.push(nimed[u]);
      uus2.push(uus[u] / 100);
      change.push(uus2);
    }
  }
 // console.log(change);
  let lala = {
    status: null,
    change: null,
  };

 if (tagasi != 0) {
 	lala.status = "INSUFFICIENT_FUNDS";
  lala.change = [];
  console.log(lala);
  return lala;
 } 
 
 for (var q=0;q<registry.length; q++) {
 	if (registry[q] != 0) {
  	lala.status = "OPEN";
    lala.change = change;
    console.log(lala);
    return lala;
  } else {
  	lala.status = "CLOSED";
    lala.change = cid;
    console.log(lala);
    return lala;
  }
 }

  //return change;

}

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

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

For:

Object.defineProperties(Array.prototype, {
  count: {
    value: function (query) {
      var count = 0;
      for (let i = 0; i < this.length; i++) {
        if (this[i] == query)
          count++;
      }

      return count;
    }
  }
});

Make sure you put brackets on the for loop. That fixes the error for me. Let me know if that helps.

Thanks for the response.

The error doesn’t come up anymore, now there is another one:

“Cannot redefine property: count”.

However, the right answer is now printed in the console in freecodecamp, but still doesn’t pass the tests. Not sure…

Thanks!