Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

no logro entender cual es la falla de mi codigo, he consultado a demas personas y ni aun asi puedo solucionarlo. siempre me salta error de codigo

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" >
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Cash Register</title>
   </head>
   <body>
     <h1>Cash Register</h1>
     
     <label for="price">Price of Item:</label>
     <input type="number" id="price" value="19.5" readonly>

     <label for="cash">Cash provided:</label>
     <input type="number" id="cash">
     <button id="purchase-btn">Purchase</button>

     <div id="change-due"></div>

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

document.getElementById("purchase-btn").addEventListener("click", function() {
  let cash = parseFloat(document.getElementById("cash").value);

  function checkCashRegister(price, cash, cid) {
    const currencyUnits = [
      ["PENNY", 0.01],
      ["NICKEL", 0.05],
      ["DIME", 0.1],
      ["QUARTER", 0.25],
      ["ONE", 1],
      ["FIVE", 5],
      ["TEN", 10],
      ["TWENTY", 20],
      ["ONE HUNDRED", 100]
    ];

    let changeDue = cash - price;
    let totalCid = cid.reduce((sum, curr) => sum + curr[1], 0).toFixed(2);

    
    if (cash < price) {
      alert("Customer does not have enough money to purchase the item");
      return "INSUFFICIENT_FUNDS";
    }

    
    if (cash === price) {
      document.getElementById("change-due").innerText = "No change due - customer paid with exact cash";
      return "EXACT_PAYMENT";
    }

    
    if (totalCid < changeDue) {
      document.getElementById("change-due").innerText = "Status: INSUFFICIENT_FUNDS";
      return "INSUFFICIENT_FUNDS";
    }

    
    if (totalCid == changeDue) {
      let changeArray = cid.filter(curr => curr[1] > 0);

     
      changeArray.reverse();

      document.getElementById("change-due").innerText = "Status: CLOSED " + changeArray.map(change => ${change[0]}: $${change[1]}).join(" ");
      return "CLOSED";
    }

   
    let changeArray = [];
    for (let i = currencyUnits.length - 1; i >= 0; i--) {
      let currencyName = currencyUnits[i][0];
      let currencyValue = currencyUnits[i][1];
      let cidValue = cid[i][1];
      let currencyAmount = 0;

      while (changeDue >= currencyValue && cidValue > 0) {
        changeDue = (changeDue - currencyValue).toFixed(2);
        cidValue = (cidValue - currencyValue).toFixed(2);
        currencyAmount += currencyValue;
      }

      if (currencyAmount > 0) {
        changeArray.push([currencyName, currencyAmount]);
      }
    }

    if (changeDue > 0) {
      document.getElementById("change-due").innerText = "Status: INSUFFICIENT_FUNDS";
      return "INSUFFICIENT_FUNDS";
    }

    document.getElementById("change-due").innerText = "Status: OPEN " + changeArray.map(change => ${change[0]}: $${change[1]}).join(" ");
    return { status: "OPEN", change: changeArray };
  }

  checkCashRegister(price, cash, cid);
});
/* 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/130.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

hi and welcome to the forum.

For this project, what are the issues you are facing? Please give us details so we can help.
For eg. What have you tested your code with? What were the results? Were they expected or unexpected? What debugging methods have you attempted? Did you add any log statements? Did you collect any logs and what were your interpretations of the logs?

Asking someone to read and debug your code means that you have not done enough to read and debug your code yourself. Please make the attempt and discuss with us, in detail, what your attempts were and your conclusions.

nada del codigo que he escrito me funciona, sigo paso a paso las consginas que me pide el proyecto pero me tira error

It sounds like you haven’t attempted any debugging steps.
The first step you should take is press the Console button.
This will show you any error messages that you need to handle.

pido que pruebes tu con el codigo y puedas darme una solucion

The cash register project is a certification project and we cannot write the code for you. You should look at the console and examine the error messages there so you can attempt to fix your code.

We can only help with a specific question and so far you have not asked any.

que esta mal de mi codigo js?

please check the console by clicking the Console button and it will show you what is wrong there. You need to read those error messages and fix your code accordingly.

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