Cuéntanos qué está pasando:
I need help finishing this code. I’m trying to solve points 12, 17, 18 and 19. I’ve tried different ways and I still can’t solve it.
Tu código hasta el momento
let price = 19.5;
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]
];
const denominations = [
["ONE HUNDRED", 100],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["DIME", 0.1],
["NICKEL", 0.05],
["PENNY", 0.01]
];
document.getElementById('price').textContent = price.toFixed(2);
document.getElementById('purchase-btn').addEventListener('click', function () {
const cash = parseFloat(document.getElementById('cash').value);
const changeDue = cash - price;
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
document.getElementById('change-due').innerText = '';
return;
}
if (cash === price) {
document.getElementById('change-due').innerText = "No change due - customer paid with exact cash";
return;
}
const result = calculateChange(changeDue, cid);
if (result.status === "INSUFFICIENT_FUNDS") {
document.getElementById('change-due').innerText = "Status: INSUFFICIENT_FUNDS";
} else if (result.status === "CLOSED") {
let changeString = `Status: CLOSED\n`;
result.change.forEach(item => {
changeString += `${item[0]}: $${item[1].toFixed(2)}\n`;
});
document.getElementById('change-due').innerText = changeString.trim();
} else if (result.status === "OPEN") {
let changeString = "Status: OPEN\n";
result.change.forEach(item => {
changeString += `${item[0]}: $${item[1].toFixed(2)}\n`;
});
document.getElementById('change-due').innerText = changeString.trim();
}
});
function calculateChange(changeDue, cid) {
let change = [];
let totalCid = 0;
for (let i = 0; i < cid.length; i++) {
totalCid += cid[i][1];
}
if (totalCid < changeDue) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
if (totalCid === changeDue) {
return { status: "CLOSED", change: cid };
}
for (let i = 0; i < denominations.length; i++) {
const denom = denominations[i];
const denomName = denom[0];
const denomValue = denom[1];
const denomAmount = cid[i][1];
let changeForThisDenom = 0;
while (changeDue >= denomValue && changeForThisDenom < denomAmount) {
changeForThisDenom += denomValue;
changeDue -= denomValue;
changeDue = parseFloat(changeDue.toFixed(2));
}
if (changeForThisDenom > 0) {
change.push([denomName, changeForThisDenom]);
}
}
if (changeDue > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
return { status: "OPEN", change: change };
}
<!-- file: index.html -->
/* file: script.js */
/* file: styles.css */
Información de tu navegador:
El agente de usuario es: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Información del Desafío:
Construye un proyecto de una caja registradora - Build a Cash Register