Tell us what’s happening:
i can not pass the last 4 test, although i’ve tested them and they’re working
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">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Cash Register Project</h1>
<p>Enter cash from customer:</p>
<input type="number" id="cash"/>
<div id="change-due"></div>
<button id="purchase-btn">Purchase</button>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
body{
display:flex;
flex-direction:column;
padding:1rem;
text-align:center;
align-items:center;
gap:5px;
}
/* file: script.js */
let price = 3.26;
let cashInput = document.getElementById('cash')
const checkoutBtn = document.getElementById("purchase-btn")
const changeDue = document.getElementById("change-due")
const changeArr = {}
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 alteredCid = [
[cid[0][0], cid[0][1], 0.01],
[cid[1][0], cid[1][1], 0.05],
[cid[2][0], cid[2][1], 0.1],
[cid[3][0], cid[3][1], 0.25],
[cid[4][0], cid[4][1], 1],
[cid[5][0], cid[5][1], 5],
[cid[6][0], cid[6][1], 10],
[cid[7][0], cid[7][1], 20],
[cid[8][0], cid[8][1], 100, 100]
];
const insuff = () => {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
}
let newCid = alteredCid.reverse()
const CalcDue = (change) => {
for (let i = 0; i < newCid.length; i++) {
while (newCid[i][1] > 0 && change > 0 && parseInt(change / newCid[i][2])) {
change -= newCid[i][2]
newCid[i][1] -= newCid[i][2]
changeArr[newCid[i][0]] = changeArr[newCid[i][0]] ? changeArr[newCid[i][0]] + newCid[i][2] : newCid[i][2]
if (change < 0.01 && change > 0) {
change -= newCid[i][2]
newCid[i][1] -= newCid[i][2]
changeArr[newCid[i][0]] = changeArr[newCid[i][0]] ? changeArr[newCid[i][0]] + newCid[i][2] : newCid[i][2]
}
}
}
if (change <= 0) {
if (newCid.reduce((a, b) => a + b[1], 0) < 0.01) {
changeDue.textContent = `Status: CLOSED ${Object.keys(changeArr).map((cur, index) => `${cur}: $${parseFloat(Object.values(changeArr)[index].toFixed(2))}`).join(' ')}`
} else {
changeDue.textContent = `Status: OPEN ${Object.keys(changeArr).map((cur, index) => `${cur}: $${Object.values(changeArr)[index]}`).join(' ')}`
}
} else {
insuff()
}
}
checkoutBtn.addEventListener("click", () => {
let cash = Number(cashInput.value)
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 change = cash - parseFloat(price)
if (newCid.reduce((a, b) => a + b[1], 0) >= change) {
CalcDue(change)
}
else {
insuff()
}
}
})
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
Challenge Information:
Build a Cash Register Project - Build a Cash Register