CASH REGISTER. Its working but still fail to pass the test

Hello.
My cash register is working properly. But when I run the tests, it says : " When price is 19.5 , the value in the #cash element is 20 , cid is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]] , and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: CLOSED PENNY: $0.5"
But it is not true! I rewrite the cid and the price, and it works. I don’t know why I keep failing the test.
Can anyone help?

My code:

for (let i = monedas.length - 1; i >= 0; i--) {
      let monedaNombre = monedas[i][0];
      let monedaValor = monedas[i][1];
      let cantidadMonedas = cid.find(moneda => moneda[0] === monedaNombre)[1] / monedaValor;

      let cantidadNecesaria = Math.floor(cambio / monedaValor);
      let totalValor = 0;

      if (cantidadNecesaria > 0) {
        if (cantidadNecesaria <= cantidadMonedas) {
          totalValor = cantidadNecesaria * monedaValor;
          cambio -= totalValor;
          cambioDetalle.push([monedaNombre, totalValor.toFixed(2)]);
        } else {
          totalValor = cantidadMonedas * monedaValor;
          cambio -= totalValor;
          if (cantidadMonedas !== 0) {
            cambioDetalle.push([monedaNombre, totalValor.toFixed(2)]);
          }
        }
      }
      cambio = Math.round(cambio * 100) / 100;
    }

    if (cambio > 0) {
      changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
      changeDue.style.display = "block";
      return;
    }

    let montoEnCajaFinal = montoEnCajaInicial - (valorCash - price);

    if (montoEnCajaFinal === 0) {
      changeDue.innerHTML = `<p>Status: CLOSED</p><p>${cambioDetalle.map(par => par.join(': $')).join(', ')}</p>`;
      changeDue.style.display = "block";
    } else {
      changeDue.innerHTML = `<p>Status: OPEN</p><p>${cambioDetalle.map(par => par.join(': $')).join(', ')}</p>`;
      changeDue.style.display = "block";
    }
  }

  calcularCambio(price, valorCash, cid);
});

Please include a link to the project description for people to read it who may want to respond to you.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cash Register</title>
    <link rel="stylesheet" href="./styles.css" />
  </head>
  <body>
    <header>
    <h1 class="title">Cash Register</h1>
    </header>
    <main>
  <div>
    <h2>Precio:<span id="precio"></span> </h2></div>
  <div class="cash">
    <h2> Efectivo entregado: </h2>
    <input id="cash" type="text" required></input>
    <button id="purchase-btn"> Purchase </button>
    </div>
    <div id="change-due">
  
     </div>
      <div id="caja"></div>  

  </main>
    <script src="./script.js"></script>
  </body>
</html>
let price = 3.26;
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 cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");

const precio = document.getElementById("precio");
const caja = document.getElementById("caja");
const status = document.getElementById("status");

precio.innerHTML = `<p>${price} $ </p>`;

function sumarElementos(cid) {
  let suma = 0;
  for (let i = 0; i < cid.length; i++) { 
    suma += cid[i][1];
  }
  return suma;
}

const suma = sumarElementos(cid);

caja.innerHTML = `<div class="monedas">
${cid.map(valor => `<p>${valor[0]}: ${valor[1]}$</p>`).join('')}
</div>`;

caja.innerHTML += `<div class="total"><p>TOTAL EN CAJA: ${suma.toFixed(2)}</p></div>`;

purchaseBtn.addEventListener("click", () => {
  const valorCash = parseFloat(cash.value);

  function calcularCambio(price, valorCash, cid) {
    let cambio = valorCash - price;
    if (cambio < 0) {
      alert("Customer does not have enough money to purchase the item");
      return;
    } else if (cambio === 0) {
      changeDue.innerHTML = "No change due - customer paid with exact cash";
      changeDue.style.display = "block";
      return;
    }

    if (cambio > suma) {
      changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
      changeDue.style.display = "block";
      return;
    }

    let cambioDetalle = [];
    const monedas = [
      ["PENNY", 0.01],
      ["NICKEL", 0.05],
      ["DIME", 0.10],
      ["QUARTER", 0.25],
      ["ONE", 1.00],
      ["FIVE", 5.00],
      ["TEN", 10.00],
      ["TWENTY", 20.00],
      ["ONE HUNDRED", 100.00]
    ];
    let montoEnCajaInicial = suma;
    let cambioOriginal = cambio;

    for (let i = monedas.length - 1; i >= 0; i--) {
      let monedaNombre = monedas[i][0];
      let monedaValor = monedas[i][1];
      let cantidadMonedas = cid.find(moneda => moneda[0] === monedaNombre)[1] / monedaValor;

      let cantidadNecesaria = Math.floor(cambio / monedaValor);
      let totalValor = 0;

      if (cantidadNecesaria > 0) {
        if (cantidadNecesaria <= cantidadMonedas) {
          totalValor = cantidadNecesaria * monedaValor;
          cambio -= totalValor;
          cambioDetalle.push([monedaNombre, totalValor.toFixed(2)]);
        } else {
          totalValor = cantidadMonedas * monedaValor;
          cambio -= totalValor;
          if (cantidadMonedas !== 0) {
            cambioDetalle.push([monedaNombre, totalValor.toFixed(2)]);
          }
        }
      }
      cambio = Math.round(cambio * 100) / 100;
    }

    if (cambio > 0) {
      changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
      changeDue.style.display = "block";
      return;
    }

    let montoEnCajaFinal = montoEnCajaInicial - (valorCash - price);

    if (montoEnCajaFinal === 0) {
      changeDue.innerHTML = `<p>Status: CLOSED</p><p>${cambioDetalle.map(par => par.join(': $')).join(', ')}</p>`;
      changeDue.style.display = "block";
    } else {
      changeDue.innerHTML = `<p>Status: OPEN</p><p>${cambioDetalle.map(par => par.join(': $')).join(', ')}</p>`;
      changeDue.style.display = "block";
    }
  }

  calcularCambio(price, valorCash, cid);
});

Please include the link by editing your first post so people can easily help you.

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

Please move all globally scope code into a function that runs when the user clicks the button. This line of code is globally scoped. Only cid and price should be in the global scope (or only static variables that never change should be there)

All the above code needs to move also.