Build a Cash Register Project

const input = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn');
const changeDue = document.getElementById('change-due');
const cashInDrawer = document.getElementById('cash-in-drawer');

let price = 1.87;
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 denominations = [
  ['PENNY', 0.01],
  ['NICKEL', 0.05],
  ['DIME', 0.10],
  ['QUARTER', 0.25],
  ['ONE', 1],
  ['FIVE', 5],
  ['TEN', 10],
  ['TWENTY', 20],
  ['ONE HUNDRED', 100]
];

let totalCashEnCaja = cid.reduce((acc, el) => acc + el[1], 0);
totalCashEnCaja = Math.round(totalCashEnCaja * 100) / 100;

let cambioParaDar = 0;
let monedasUsadas = {};

const ticket = () => {
  const monedas = Object.keys(monedasUsadas);
  const cantidadDeMonedas = Object.values(monedasUsadas);
  let ticketHTML = "Status: OPEN";

  cantidadDeMonedas.forEach((cantidad, index) => {
    const x = denominations.findIndex(el => el[0] === monedas[index]);
    if (cantidad) {
      ticketHTML += ` ${monedas[index]}: $${denominations[x][1] * cantidad}`;
    };
  });
  changeDue.textContent = ticketHTML;
}

const darCambio = (cashDeCliente, cajero, monedas) => {
  let cambioCorrespondiente = cashDeCliente - price;
  cajero.reverse();
  monedas.reverse();

  for (let i= 0; i < monedas.length; i++) {
    if (monedas[i][1] < cambioCorrespondiente) {
      while(cajero[i][1] > 0 && parseFloat(cambioParaDar.toFixed(2)) < cambioCorrespondiente) {
        cambioParaDar += monedas[i][1];
        cajero[i][1] -= monedas[i][1];
        monedasUsadas[monedas[i][0]] = (monedasUsadas[monedas[i][0]] || 0) + 1;
        console.log(monedasUsadas)
        console.log(parseFloat(cambioParaDar.toFixed(2)))
      }
      while(parseFloat(cambioParaDar.toFixed(2)) > cambioCorrespondiente) {
        cambioParaDar -= monedas[i][1];
        cajero[i][1] += monedas[i][1];
        monedasUsadas[monedas[i][0]] = monedasUsadas[monedas[i][0]] - 1;
        console.log(monedasUsadas)
        console.log(parseFloat(cambioParaDar.toFixed(2)))
      }
     if (parseFloat(cambioParaDar.toFixed(2)) === cambioCorrespondiente) {
      return ticket();
    }
    }
  }
}

const actualizarCaja = () => {
  const cashElement = cid.map(el => `<p>${el[0]} : $${el[1]}</p>`).join("");
  cashInDrawer.innerHTML = cashElement;
}
actualizarCaja()

const validarInput = (cashCliente) => {
  if (cashCliente < price) {
    alert('Customer does not have enough money to purchase the item');
    return;
  } else if (cashCliente === price) {
    changeDue.textContent = 'No change due - customer paid with exact cash';
    return;
  }
}

purchaseBtn.addEventListener('click', () => {
  validarInput(Number(input.value));
  darCambio(Number(input.value), cid, denominations);
});

la funcion ‘ticket()’ cumple con mostrar en changeDue las monedas o billetes usados, incluso cuando el precio es 3.26 como lo indica la instruccion numero 7 del proyecto. Funciona correctamente, sin embargo el curso me lo marca como error. Pueden decirme que podria estar pasando? tambien intente agregar varias etiquetas ‘p’ con el debido contenido pero aun asi me marca error.

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 (').

the code above is in the global scope.
This code will only run once and never again during the testcase run.
This can cause your code to fail the tests.

Move the code inside the local function scope of the function that runs when the purchase button is clicked instead.

const input = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn');
const changeDue = document.getElementById('change-due');
const cashInDrawer = document.getElementById('cash-in-drawer');

let price = 1.87;
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 denominations = [
  ['PENNY', 0.01],
  ['NICKEL', 0.05],
  ['DIME', 0.10],
  ['QUARTER', 0.25],
  ['ONE', 1],
  ['FIVE', 5],
  ['TEN', 10],
  ['TWENTY', 20],
  ['ONE HUNDRED', 100]
];

cid.reverse();
denominations.reverse();

const actualizarCaja = (cajero) => {
  const cashElement = cajero.map(el => `<p>${el[0]} : $${parseFloat(el[1].toFixed(2))}</p>`).join("");
  cashInDrawer.innerHTML = cashElement;
}

const ticket = (infoDeCambio) => {
  const monedas = Object.keys(infoDeCambio);
  const cantidadDeMonedas = Object.values(infoDeCambio);
  changeDue.textContent = "Status: OPEN";

  cantidadDeMonedas.forEach((cantidad, index) => {
    const x = denominations.findIndex(el => el[0] === monedas[index]);
    if (cantidad) {
      changeDue.textContent += ` ${monedas[index]}: $${denominations[x][1] * cantidad}`;
    };
  });
}

const darCambio = (cashDeCliente, cajero, monedas) => {
  let totalCashEnCaja = cid.reduce((acc, el) => acc + el[1], 0);
  totalCashEnCaja = Math.round(totalCashEnCaja * 100) / 100;

  let cambioCorrespondiente = cashDeCliente - price;

  let cambioParaDar = 0;
  let monedasUsadas = {};

  for (let i= 0; i < monedas.length; i++) {
    if (monedas[i][1] < cambioCorrespondiente) {
        while(cajero[i][1] > 0 && parseFloat(cambioParaDar.toFixed(2)) < cambioCorrespondiente) {
          cambioParaDar += monedas[i][1];
          cajero[i][1] -= monedas[i][1];
          monedasUsadas[monedas[i][0]] = (monedasUsadas[monedas[i][0]] || 0) + 1;
        };
        
        while(parseFloat(cambioParaDar.toFixed(2)) > cambioCorrespondiente) {
           cambioParaDar -= monedas[i][1];
           cajero[i][1] += monedas[i][1];
           monedasUsadas[monedas[i][0]] = monedasUsadas[monedas[i][0]] - 1;
        };

      
    }
    if (Math.abs(parseFloat(cambioParaDar.toFixed(2)) - cambioCorrespondiente) < 0.01) {
      ticket(monedasUsadas);
      return
    }
  }
}


const validarInput = (cashCliente) => {
  if (cashCliente < price) {
    alert('Customer does not have enough money to purchase the item');
    return;
  } else if (cashCliente === price) {
    changeDue.textContent = 'No change due - customer paid with exact cash';
    return;
  }
}

purchaseBtn.addEventListener('click', () => {
  validarInput(Number(input.value));
  darCambio(Number(input.value), cid, denominations);
  actualizarCaja(cid);
  input.value = "";
});
``` puede corroborrar que funciona.

did you have a question?
I noticed the new code you posted still has code in the global scope.

Globals are just ‘denominations’ and the price.
Should I put them inside the function?

This is running in the global scope so for the test cases, only the first testcase will run this.

1 Like