Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

necesito ayuda con un codigo de la caja registradora java script. Especificamente el tema 18 y 19

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Caja Registradora</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>

  <!-- Input para el efectivo proporcionado -->
  <label for="cash">Efectivo proporcionado: </label>
  <input type="number" id="cash" name="cash" step="0.01">

  <!-- Botón para realizar la compra -->
  <button id="purchase-btn">Comprar</button>

  <!-- Elemento para mostrar el cambio adeudado -->
  <div id="change-due"></div>

  <script src="script.js"></script>
</body>
</html>
/* file: script.js */
// Variables globales
let price = 19.5; // Precio del artículo
let cid = [
  ["PENNY", 0.5],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];

// Denominaciones
const DENOMINATIONS = {
  "PENNY": 0.01,
  "NICKEL": 0.05,
  "DIME": 0.1,
  "QUARTER": 0.25,
  "ONE": 1,
  "FIVE": 5,
  "TEN": 10,
  "TWENTY": 20,
  "ONE HUNDRED": 100,
};

// Función para manejar la compra
function checkCashRegister(price, cash, cid) {
  let changeDue = cash - price;
  let totalInDrawer = cid.reduce((sum, [_, amount]) => sum + amount, 0);

  totalInDrawer = Math.round(totalInDrawer * 100) / 100; // Evita errores de precisión

  // Verifica si el cliente tiene suficiente efectivo
  if (cash < price) {
    alert("Customer does not have enough money to purchase the item");
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }

  // Caso de pago exacto
  if (changeDue === 0) {
    return { status: "EXACT_PAYMENT", change: [] };
  }

  // Caso en que no hay suficiente dinero en el cajón
  if (totalInDrawer < changeDue) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }

  // Caso en que el dinero total en el cajón es igual al cambio debido
  if (totalInDrawer === changeDue) {
    return { status: "CLOSED", change: [...cid] };
  }

  // Calcular el cambio a devolver
  let change = [];
  for (let [denomination, amount] of [...cid].reverse()) {
    let value = DENOMINATIONS[denomination];
    let toGive = 0;

    while (changeDue >= value && amount > 0) {
      toGive += value;
      changeDue -= value;
      changeDue = Math.round(changeDue * 100) / 100; // Evita errores de precisión
      amount -= value;
    }

    if (toGive > 0) {
      change.push([denomination, toGive]);
    }
  }

  // Si no se puede devolver el cambio exacto
  if (changeDue > 0) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  }

  return { status: "OPEN", change };
}

// Manejo del evento click en el botón de compra
document.getElementById("purchase-btn").addEventListener("click", () => {
  const cashInput = document.getElementById("cash").value;
  const cash = parseFloat(cashInput);

  if (isNaN(cash) || cash <= 0) {
    alert("Por favor, ingrese un valor válido de efectivo.");
    return;
  }

  const result = checkCashRegister(price, cash, cid);
  const changeDueElement = document.getElementById("change-due");

  if (result.status === "INSUFFICIENT_FUNDS") {
    changeDueElement.textContent = "Status: INSUFFICIENT_FUNDS";
  } else if (result.status === "CLOSED") {
    
    changeDueElement.textContent = `Status: CLOSED  ${result.change.map(c => `${c[0]}: $${c[1].toFixed(2)}`).join(" Status: CLOSED PENNY: $0.5")}`;
  } else if (result.status === "OPEN") {
    
    changeDueElement.textContent = `Status: OPEN ${result.change.map(c => `${c[0]}: $${c[1].toFixed(2)}`).join("Status: CLOSED ")}`;
  } else if (result.status === "EXACT_PAYMENT") {
    changeDueElement.textContent = "No change due - customer paid with exact cash";
  }
});
/* file: styles.css */
body {
  font-family: Arial, sans-serif;
  margin: 20px;
}

#change-due {
  margin-top: 20px;
  font-size: 18px;
  font-weight: bold;
}

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

The output of your app is
Status: OPEN TWENTY: $60.00Status: CLOSED TEN: $20.00Status: CLOSED FIVE: $15.00Status: CLOSED ONE: $1.00Status: CLOSED QUARTER: $0.50Status: CLOSED DIME: $0.20Status: CLOSED PENNY: $0.04

the Status part should be there only once

also if a denominatio is 0 you should not include it. If you have NICKEL: $0.00 it should not be there