Tell us what’s happening:
I have finished writing the code and it is responding very well. But when I run the test I get failed tests that are unclear to me. What could be the issue?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Build a Cash Register Project</title>
<link rel="stylesheet" href="./styles.css" />
</head>
<body>
<div id="change-due"></div>
<input id="cash" />
<div id="change-due"></div>
<button id="purchase-btn">Balance</button>
<div id="price"></div>
<div id="balance"></div>
<h4>Change in drawer</h4>
<div id="drawerChange"></div>
<script src="./script.js"></script>
</body>
</html>
/* file: script.js */
const cash = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const drawerChange = document.getElementById("drawerChange");
const pri = document.getElementById("price");
const bal = document.getElementById("balance");
let price = 19.5;
let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
let cUnit = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.10],
['QUARTER', 0.25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
pri.innerHTML = price
let regTotal = 0;
const updateDrawerDisplay = () => {
drawerChange.innerHTML = "";
regTotal = 0;
for (let i = 0; i < cid.length; i++) {
drawerChange.innerHTML += `<p>${cid[i][0]} : <span>${cid[i][1]}</span></p>`;
regTotal += cid[i][1];
}
return parseFloat(regTotal.toFixed(2))
};
updateDrawerDisplay();
const calBalance = () => {
let cashAmount = parseFloat(cash.value);
if (cashAmount === price) {
changeDue.innerHTML = "";
changeDue.innerHTML += `<p>No change due - customer paid with exact cash</p>`;
cash.value = ""
return
}
let balance = parseFloat((cash.value - price).toFixed(2));
bal.innerHTML = "";
bal.innerHTML = `<p>${balance}</p>`;
regTotal = parseFloat(regTotal.toFixed(2))
if (regTotal < balance) {
changeDue.innerHTML = "";
changeDue.innerHTML += `<p>Status: INSUFFICIENT_FUNDS</p>`;
return;
} else if (regTotal === balance) {
let changeResult = calculateChange(balance, cid, cUnit);
regTotal = 0;
for (let i = 0; i < cid.length; i++) {
cid[i][1] = 0;
}
updateDrawerDisplay();
changeDue.innerHTML = "";
changeDue.innerHTML = "Status: CLOSED " + changeResult.map((cash) => `${cash[0]} : $${cash[1]}`).join(" ")
// changeDue.innerHTML += "Status: CLOSED";
// for (let i = 0; i < changeResult.length; i++) {
// changeDue.innerHTML += `<p>${changeResult[i][0]} : ${changeResult[i][1]}</p>`;
// }
return;
}
let changeResult = calculateChange(balance, cid, cUnit);
changeDue.innerHTML = "";
changeDue.innerHTML = "Status: <b>OPEN</b> <br><br> " + changeResult.map((cash) => `<b>${cash[0]}</b> : $${cash[1]}<br>`).join(" ")
// changeDue.innerHTML += "Status: OPEN";
// for (let i = 0; i < changeResult.length; i++) {
// changeDue.innerHTML += `<p>${changeResult[i][0]} : ${changeResult[i][1]}</p>`;
// }
updateDrawerDisplay();
// Reset cash input
cash.value = "";
};
const calculateChange = (balance, cid, cUnit) => {
let getTotalbalance = [];
for (let i = cUnit.length - 1; i >= 0; i--) {
let totalAmount = 0;
while (balance >= cUnit[i][1] && cid[i][1] >= cUnit[i][1]) {
totalAmount = parseFloat((totalAmount + cUnit[i][1]).toFixed(2));
cid[i][1] = parseFloat((cid[i][1] - cUnit[i][1]).toFixed(2));
balance = parseFloat((balance - cUnit[i][1]).toFixed(2));
}
if (totalAmount > 0) {
getTotalbalance.push([cUnit[i][0], totalAmount]);
}
}
return getTotalbalance;
}
purchaseBtn.addEventListener('click', () => {
if (cash.value < price) {
alert("Customer does not have enough money to purchase the item");
} else {
calBalance();
}
});
cash.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
if (cash.value < price) {
alert("Customer does not have enough money to purchase the item");
} else {
calBalance();
}
}
});
/* 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/128.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register