Cash Register bug with +=

Tell us what’s happening:
Hey guys, I’ve been stuck with this bug for a few hours and I’m really unsure what to do.
The issue is towards the bottom, I have 2 variables - an array called changeGiven which is checked if it already has the current currency I’m on.

If it does contain that currency, then it should add the currency total, which should just be $20, however when using the += operator, it seems to increase both the currency array, and the changeGiven array.

Where am I going wrong? :frowning:

Your code so far

function checkCashRegister(price, cash, cid) {
  let currency = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["DOLLAR", 1],
    ["FIVE DOLLARS", 5],
    ["TEN DOLLARS", 10],
    ["TWENTY DOLLARS", 20],
    ["ONE-HUNDRED DOLLARS", 100]
  ]
  let statuses = ["OPEN", "CLOSED", "INSUFFICIENT_FUNDS"]
  let changeNeeded = cash - price
  let index = cid.length - 1
  let changeGiven = []
  let result = {status: "", change: []}
  let finalCid = 0

  while (changeNeeded > 0) {
    if (index == 0 && cid[index][1] == 0 && changeNeeded > 0) {
      changeGiven = []
      result.status = statuses[2]
      result.change = changeGiven
      return result
    }
    else if (changeNeeded - currency[index][1] < 0 || cid[index][1] == 0) {
      index--
    } 
    else if (changeNeeded - currency[index][1] >= 0 && cid[index][1] > 0) {
      changeNeeded -= currency[index][1]
      changeNeeded = parseFloat(changeNeeded.toFixed(2))
      cid[index][1] -= currency[index][1]

      if (changeGiven.indexOf(currency[index]) > -1) {
        let arrIndex = changeGiven.indexOf(currency[index])
        changeGiven[arrIndex][1] += currency[index][1]
        console.log(currency[index])
      } else {
        changeGiven.push(currency[index])
      }
      if (cid[index][1] == 0) {
        index--
      }
    } 
  }

  for (let i = 0; i < cid.length; i++) {
    finalCid += cid[i][1]
  }
  if (changeNeeded == 0 && finalCid > 0) {
    result.status = statuses[0]
    result.change = changeGiven
    console.log(result)
    return result
  } else {
    result.status = statuses[1]
    result.change = changeGiven
    return result
  }
}

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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: {{challengeTitle}} JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

You might want to search for information regarding copying an array. I believe the line giving you grief might be this one (I’m not 100% without doing some testing):

changeGiven.push(currency[index])

So arrays are tricky, because the variable for an array doesn’t store the array itself, but rather stores a pointer to the array. Because of this, if you try to copy the array, what you end up doing is just copying the pointer, and you end up with two variables pointing to the same array:

let x = [ 1, 2, 3 ];
let y = x; 
y[0] = 5;
console.log(y)   // prints [ 5, 2, 3 ]
console.log(x)   // surprisingly also prints [5, 2, 3]

So in your code, as currenty[index] is an array, when you push it into changeGiven, now changeGiven contains a pointer back to your original array inside currency[index], and modifying one is the same as modifying the other.

There are tricks to get around this you may want to look into… but one of the simplest is don’t copy an array unless you need to. In your code theres many ways for your to deal with this issue, one requiring the least amount of changes would be to use the currency array values to create a new array rather than attempt to copy the array:

IF YOURE NOT SURE WHAT I MEAN:

changeGiven.push( [ currency[index][0], currency[index][1] ] )

Hope that all makes sense.

2 Likes

Thank you so much, I didn’t realise it was just becoming a pointer. I just have to find a way to add the total instead of pushing the same item to the array and hopefully will be done.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.