Tell us what’s happening:
Hello, I can’t solve task #7, I tried to manually set price and cid to exactly same values and everything seems like it’s working fine, but code can’t pass the test. Can anyone give me some clues how to fix this?
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>Cash Register</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<input type="text" id="cash">
<div id="change-due"></div>
<button id="purchase-btn">Buy</button>
<div id="cid-container">
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
/* 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 changeArr = [
[0.01, 0],
[0.05, 0],
[0.1, 0],
[0.25, 0],
[1.0, 0],
[5.0, 0],
[10.0, 0],
[20.0, 0],
[100.0,0]
]
const changedueDiv = document.getElementById("change-due");
document.getElementById("purchase-btn").addEventListener("click", () => {
const cashInput = Number(document.getElementById("cash").value);
if (cashInput < price) {
alert("Customer does not have enough money to purchase the item");
return;
} else if (cashInput === price) {
changedueDiv.innerText = "No change due - customer paid with exact cash";
return;
}
let changeValue = cashInput - price;
let changeResult = change(changeArr, changeValue)
displayChange(changeResult);
displayCID()
})
function change(arr, changeValue) {
const cashInput = Number(document.getElementById("cash").value);
let totalCid = cid.reduce((total, amount) => total + amount[1], 0).toFixed(2);
for (let i = arr.length - 1; i >= 0; i--) {
while (changeValue >= arr[i][0] && cid[i][1] > 0) {
changeValue -= arr[i][0];
changeValue = Number((changeValue).toFixed(2));
cid[i][1] -= arr[i][0];
cid[i][1] = Number((cid[i][1]).toFixed(2));
arr[i][1] += arr[i][0];
}
}
if (changeValue > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
} else if(Number(totalCid)+Number(price) === cashInput){
return { status: "CLOSED", change: arr };
}else if (changeValue === 0 && totalCid > 0) {
return { status: "OPEN", change: arr };
}
}
function displayCID() {
const cidContainer = document.getElementById("cid-container");
cidContainer.innerHTML = "";
cid.forEach(denomination => {
const div = document.createElement("div");
div.textContent = `${denomination[0]}: $${denomination[1].toFixed(2)}`;
cidContainer.appendChild(div);
});
}
function displayChange(changeResult) {
const changeContainer = document.getElementById("change-due");
changeContainer.innerHTML = "";
const denominationNames = {
0.01: "PENNY",
0.05: "NICKEL",
0.1: "DIME",
0.25: "QUARTER",
1: "ONE",
5: "FIVE",
10: "TEN",
20: "TWENTY",
100: "ONE HUNDRED"
};
const statusDiv = document.createElement("p");
statusDiv.textContent += `Status: ${changeResult.status}`;
changeContainer.appendChild(statusDiv);
if (changeResult.status === "OPEN" || changeResult.status === "CLOSED") {
const changeGiven = changeResult.change.filter(item => item[1] > 0);
changeGiven.reverse().forEach(denomination => {
const name = denominationNames[denomination[0]];
const count = denomination[1] < 1 ? parseFloat(denomination[1].toFixed(2)) : denomination[1];
changeContainer.textContent += ` ${name}: $${count} `;
});
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register