Tell us what’s happening:
Hello, I am stuck in step 12 and 13. When I enter different cash amounts, I see that the change is always correct,
I have tried to work with the float values and maybe the mistake was there but no luck.
Thanks,
Your code so far
<!-- file: index.html -->
<input id="cash"></input>
<div id="change-due"></div>
<button id="purchase-btn">Comprar</button>
<script src="script.js"></script>
/* 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 billete100 = 0;
let billete20 = 0;
let billete10 = 0;
let billete5 = 0;
let billete1 = 0;
let billeteQuarter = 0;
let billeteDime = 0;
let billeteNickel = 0;
let billetePenny = 0;
const purchaseButton = document.getElementById("purchase-btn");
const textoResultado = document.getElementById("change-due");
purchaseButton.addEventListener("click", () => {
// Obtengo el dinero que saco.
let cashTexto = document.getElementById("cash").value;
console.log(cashTexto);
let cash = parseFloat(cashTexto);
console.log(cash);
let priceFloat = parseFloat(price)
//Hago la diferencia
let dineritoDevolver = Math.round((cash - priceFloat) * 100) / 100;
console.log("dinero a devolver= " + dineritoDevolver);
let dineroCaja = cid[8][1] + cid[7][1] +cid[6][1]+cid[5][1]+cid[4][1]+cid[3][1]+cid[2][1]+cid[1][1]+cid[0][1]
if (dineritoDevolver < 0) {
alert("Customer does not have enough money to purchase the item");
}
else if (dineritoDevolver == 0) {
textoResultado.innerText = "No change due - customer paid with exact cash";
}
else {
calcularDevolucion(dineritoDevolver);
let cadenaTexto = ["Status: OPEN "]
if (billete100 > 0) {
cadenaTexto.push("ONE HUNDRED: $" + (billete100 * 100));
}
if (billete20 > 0) {
cadenaTexto.push("TWENTY: $" + (billete20 * 20));
}
if (billete10 > 0) {
cadenaTexto.push("TEN: $" + (billete10 * 10));
}
if (billete5 > 0) {
cadenaTexto.push("FIVE: $" + (billete5 * 5));
}
if (billete1 > 0) {
cadenaTexto.push("ONE: $" + (billete1 * 1));
}
if (billeteQuarter > 0) {
cadenaTexto.push("QUARTER: $" + (billeteQuarter * 0.25));
}
if (billeteDime > 0) {
cadenaTexto.push("DIME: $" + (billeteDime * 0.10));
}
if (billeteNickel > 0) {
cadenaTexto.push("NICKEL: $" + (billeteNickel * 0.05));
}
if (billetePenny > 0) {
cadenaTexto.push("PENNY: $" + (billetePenny * 0.01));
}
textoResultado.innerText = cadenaTexto.join("\n");
}
});
function calcularDevolucion(cash) {
// Mientras sea mayor que 0, sigo iterando.
while (cash > 0) {
if (cid[8][1] >= 100 && cash >= 100) {
cash = Math.round((cash - 100) * 100) / 100;
cid[8][1] = Math.round((cid[8][1] - 100) * 100) / 100;
billete100++;
}
else if (cid[7][1] >= 20 && cash >= 20) {
cash = Math.round((cash - 20) * 100) / 100;
cid[7][1] = Math.round((cid[7][1] - 20) * 100) / 100;
billete20++;
}
else if (cid[6][1] >= 10 && cash >= 10) {
cash = Math.round((cash - 10) * 100) / 100;
cid[6][1] = Math.round((cid[6][1] - 10) * 100) / 100;
billete10++;
}
else if (cid[5][1] >= 5 && cash >= 5) {
cash = Math.round((cash - 5) * 100) / 100;
cid[5][1] = Math.round((cid[5][1] - 5) * 100) / 100;
billete5++;
}
else if (cid[4][1] >= 1 && cash >= 1) {
cash = Math.round((cash - 1) * 100) / 100;
cid[4][1] = Math.round((cid[4][1] - 1) * 100) / 100;
billete1++;
}
else if (cid[3][1] >= 0.25 && cash >= 0.25) {
cash = Math.round((cash - 0.25) * 100) / 100;
cid[3][1] = Math.round((cid[3][1] - 0.25) * 100) / 100;
billeteQuarter++;
}
else if (cid[2][1] >= 0.10 && cash >= 0.10) {
cash = Math.round((cash - 0.10) * 100) / 100;
cid[2][1] = Math.round((cid[2][1] - 0.10) * 100) / 100;
billeteDime++;
}
else if (cid[1][1] >= 0.05 && cash >= 0.05) {
cash = Math.round((cash - 0.05) * 100) / 100;
cid[1][1] = Math.round((cid[1][1] - 0.05) * 100) / 100;
billeteNickel++;
}
else if (cid[0][1] >= 0.01 && cash >= 0.01) {
cash = Math.round((cash - 0.01) * 100) / 100;
cid[0][1] = Math.round((cid[0][1] - 0.01) * 100) / 100;
billetePenny++;
}
else {
return
}
}
};
/* 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/138.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register