Build a Cash Register

I made the lesson for Build a cash register project and the code seems ok, but the test 12 and 18 don’t pass. ¿What can be happening?

My HTML:

<html>
  <head>
    
  </head>
  <body>
    <input id="cash" type="number" value="0"/> 
    <button id="purchase-btn" type="button">Change</button>
        <p id="change-due"></p>
    <script src="script.js"></script>
  </body>
</html>

My javascript:

let price = 19.50;
let cid = [
  ['PENNY', 1.01],
  ['NICKEL', 2.05],
  ['DIME', 3.1],
  ['QUARTER', 4.25],
  ['ONE', 90],
  ['FIVE', 55],
  ['TEN', 20],
  ['TWENTY', 60],
  ['ONE HUNDRED', 100]
];


const base = [
  ['PENNY', 0.01],
  ['NICKEL', 0.05],
  ['DIME', 0.1],
  ['QUARTER', 0.25],
  ['ONE', 1],
  ['FIVE', 5],
  ['TEN', 10],
  ['TWENTY', 20],
  ['ONE HUNDRED', 100]
];



const totalChange = () => {
  let acum = 0;
  cid.forEach(item => {
    acum+=item[1];
  });

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/build-a-cash-register-project/build-a-cash-register
  return acum;
}

const button = document.getElementById('purchase-btn');

const div = document.getElementById('change-due');

const calculateChange = () => {
  let closed = true;
let input = document.getElementById('cash');
  let  cash = parseFloat(input.value);
  if(cash < price) {
    alert("Customer does not have enough money to purchase the item");
  }
  if(cash.toFixed(2) === price.toFixed(2)) {
    div.textContent = "No change due - customer paid with exact cash";
    return;
  }
  let returned = cash-price;
  let cashed = [];
  let cidReversed = cid.reverse();
  base.reverse().forEach((el, index) => {
    if(el[1] < parseFloat(returned.toFixed(2)) && returned > 0) { //Entramos si podemos dar cambio
      if(cidReversed[index][1] >= returned) { //Nos sobra
        let selected = el[1]*Math.floor((returned / el[1]) + 1e-10);
        if(selected > 0.005) {
          cashed.push([el[0],selected]);
          returned -= selected;
          if(cidReversed[index][1] > returned + selected) {
            closed = false;
          }
          for(let i = 0; i< 9; i++) {
            if(cid[i][0] === el[0]) {
              cid[i][1] -= selected;
            }
          } 
        }
      }
      else { //Nos falta
        let selected = cidReversed[index][1];
         if(selected > 0.005) {
          cashed.push([el[0],selected]);
          returned -= selected;
          for(let i = 0; i< 9; i++) {
            if(cid[i][0] === el[0]) {
              cid[i][1] = 0;
            }
          }
        }
      }
    }
  });
  if(Math.abs(parseFloat(returned.toFixed(2))) === 0.00) {
    const word = closed? "CLOSED" : "OPEN";
    div.textContent = "Status: " + word;
    cashed.forEach(el => {
      div.textContent+=" " + el[0] + ": " + "$" + el[1];
    })
  }
   else if(Math.abs(parseFloat(returned.toFixed(2))) > 0) {
      div.textContent = "Status: INSUFFICIENT_FUNDS";
    }
    console.log(cid);
}

button.addEventListener('click', calculateChange);

Thanks in advance.

can you also provide a link to the challenge/step?

i’ve edited your post to include the link to the project. Always post a link when you need help with an fCC topic.
thanks

Edit: also it seems you have 4 tests failing, not just 2?

To test your code I have simulated the failing test like this:

price = 3.26
cash.value = 100
cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

document.querySelector("#purchase-btn").click()

and it works as expected.

So I then added the previous test

cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]
price = 19.5
cash.value = 20
document.querySelector("#purchase-btn").click()

price = 3.26
cash.value = 100
cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

document.querySelector("#purchase-btn").click()

and now it gives Status: OPEN PENNY: $96.74000000000001

if this happen is because your code is relying on values that are not reset after each click of the button but are kept between clicks

you haven’t mentioned what testing you’ve attempted? For eg, when I attempted a couple of tests, the app wasn’t able to give me the correct values after the 2nd test.
Here’s what I tried:

With the code exactly as you gave it above, I typed in:

32.10

and this gave me the following (which is correct so far):
Change
Status: OPEN TEN: $10 ONE: $2 QUARTER: $0.5 NICKEL: $0.1

After that, I typed in
100
but the result was not correct as it said:

Change
Status: INSUFFICIENT_FUNDS

Please note that I tested these two, one after the other without reloading the page. Give it a try and see.

oh yes here, reverse is changing base. Do not have a global value like base that you change each time

Thank you very much! It’s the reverse() problem. I thought that reverse was inmutable. Mutability is very important!!