Tell us what’s happening:
can somone explain the last test on cash register project(the only one that didn’t accepted)
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>Document</title>
</head>
<body>
<input type="text " id="cash" />
<div id="change-due"></div>
<button id="purchase-btn">Purchase</button>
<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],
];
const amount = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100],
];
let bills = [
["PENNY", 0],
["NICKEL", 0],
["DIME", 0],
["QUARTER", 0],
["ONE", 0],
["FIVE", 0],
["TEN", 0],
["TWENTY", 0],
["ONE HUNDRED", 0],
];
document.getElementById("purchase-btn").addEventListener("click", () => {
for (let i = 0; i < bills.length; i++) {
bills[i][1] = Math.ceil(cid[i][1] / amount[i][1]);
}
let cash = document.getElementById("cash").value;
let changedue = document.getElementById("change-due");
if (cash < price) {
alert("Customer does not have enough money to purchase the item");
} else if (cash == price) {
changedue.textContent = "No change due - customer paid with exact cash";
} else {
let text = "";
let change = cash - price;
let sum = 0;
changedue.textContent = "Status: OPEN";
for (let i = amount.length - 1; i >= 0; i--) {
let j = 0;
while (change >= amount[i][1] && bills[i][1] > 0) {
j++;
change = (change - amount[i][1]).toFixed(2);
bills[i][1] -= 1;
cid[i][1] = (cid[i][1] - amount[i][1]).toFixed(2);
}
if (j > 0) {
text = text + ` ${amount[i][0]}: $${j * amount[i][1]}`;
}
}
if (change > 0) {
changedue.textContent = "Status: INSUFFICIENT_FUNDS";
} else {
for (let i = 0; i < cid.length; i++) {
sum = sum + cid[i][1];
}
console.log(cid);
if (sum == 0) {
changedue.textContent = `Status: CLOSED${text}`;
} else {
changedue.textContent = `Status: OPEN${text}`;
}
}
}
});
/* 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/126.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register