Build a Cash Register Project - Build a Cash Register

I don’t know what i’m doing wrong here. I’ve tested many times my code alone and it’s working good. But i’m failing in many tests anyway. Please help me, i’ve spent more time in this than i would like.
The tests i’m failing begins when it speak about the status. This is the first one

When price is 19.5 , the value in the #cash element is 20 , cid is [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]] , and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: OPEN QUARTER: $0.5" .

Your code so far

<!-- file: index.html -->

/* file: script.js */
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],
];
let reversedCid = [...cid].reverse();

const displayChangeDue = document.getElementById("change-due");
let cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const productPrice = document.getElementById("product-price");
const cidDisplay = document.getElementById("cash-drawer-display");

productPrice.textContent = `${price}`;

const checkPayment = (changeDue) => {
  if (!cash.value) {
    return alert(
      `There's no cash registered from the customer. Please insert the amount that the customer paid.`
    );
  }

  if (parseFloat(cash.value) === price) {
    displayChangeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`;
    return;
  }

  if (parseFloat(cash.value) < price) {
    return alert("Customer does not have enough money to purchase the item");
  }
  calculateChange(changeDue);
};

const checkCID = (changeDue) => {
  let totalCID = cid
    .map((arr) => arr[1])
    .reduce((prev, curr) => {
      // Garante que tanto prev quanto curr sejam números antes de realizar a adição
      return Number(prev) + Number(curr);
    }, 0)
    .toFixed(2);

  if (totalCID < changeDue) {
    displayChangeDue.innerHTML = `Status: INSUFFICIENT_FUNDS`;
    return;
  }
  if (totalCID === changeDue) {
    displayChangeDue.innerHTML = `Status: CLOSED`;
    checkPayment(changeDue);
  } else {
    displayChangeDue.innerHTML = `Status: OPEN`;
    checkPayment(changeDue);
  }
};

const calculateChange = (changeDue) => {
  let changeArr = [];
  const denominations = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
  denominations.sort((a, b) => b - a);
  let remainingChange = changeDue;

  for (let i = 0; i < denominations.length; i++) {
    let cash = {
      name: reversedCid[i][0],
      value: denominations[i],
      totalValue: reversedCid[i][1],
      qty: 0,
    };

    console.log("Cash Name:");
    console.log(cash.name);
    console.log("Cash Value:");
    console.log(cash.value);
    console.log("Cash Total Value");
    console.log(cash.totalValue);

    while (cash.totalValue > 0 && remainingChange >= cash.value) {
      remainingChange = (remainingChange - cash.value).toFixed(2);
      cash.qty++;
      cash.totalValue = (cash.totalValue - cash.value).toFixed(2);
      reversedCid[i][1] = (reversedCid[i][1] - cash.value).toFixed(2);
    }

    console.log("cashQty:");
    console.log(cash.qty);
    console.log("Cash Total Value after operation:");
    console.log(cash.totalValue);
    console.log("Reversed CID after operation:");
    console.log(reversedCid[i][1]);

    if (cash.qty > 0) {
      // Calcula o valor total com base na quantidade e no valor unitário
      let totalValue = cash.value * cash.qty;

      // Verifica se o valor total é um número inteiro
      if (Number.isInteger(totalValue)) {
        let intValue = Math.floor(totalValue);
        changeArr.push([cash.name, intValue]);
      } else {
        let formattedValue = parseFloat(totalValue.toFixed(2));
        changeArr.push([cash.name, formattedValue]);
      }
    }
  }

  console.log(changeArr);
  console.log(`remainingChange: ${remainingChange}`);

  if (changeArr) {
    for (let cash of arr) {
      displayChangeDue.innerHTML += `<p>${cash[0]} $${cash[1]}</p>`;
    }
  }
  console.log("UI actualized");
};

const displayCid = () => {
  cidDisplay.innerHTML = `<p><strong>Cash in Drawer:</strong></p>`;
  for (let cash of cid) {
    cidDisplay.innerHTML += `<p>${cash[0]}: $${cash[1]}</p>`;
  }
};
displayCid();

purchaseBtn.onclick = () => {
  checkCID(Number((cash.value - price).toFixed(2)));
};

cash.onkeydown = (e) => {
  if (e.key === "Enter") {
    checkCID(Number((cash.value - price).toFixed(2)));
  }
};

/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

You can’t rely on any global variables like this one (other than the price and cid given at the start).

Global variables are setup at loading time and never again and the tests run consecutively so any global variables will retain whatever values they received from the start and not get updated.

So, what i have to do is create a variable into one function for using?

just define it inside the function (not in the global scope)

i still have issues to make it through. I defined reversedCID inside of calculateChange, but i don’t achieved anymore tests

have you checked for error messages in your console?

There’s no error messagens in my console. Just the failed tests

Then if there are no syntax issues the next step is to add some console logs statements to try to figure out why your code is failing.