Tell us what’s happening:
the code is working why it is not passing the conditions anyone please
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 App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Cash Register App</h1>
<div class="container">
<h3>Enter cash from customer :</h3>
<input type="number" id="cash">
<button id="purchase-btn">Purchase</button>
<div id="cash-container">
<h3>change in the Drawer</h3>
</div>
<div id="change-due"></div>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
body{
background-color: rgb(19, 19, 46);
color: white;
}
h1{
text-align: center;
}
.container{
display: flex;
align-items: center;
flex-direction: column;
}
button{
margin: 7px auto 5px;
background-color: rgb(209, 59, 13);
color: white;
border-radius: 1px;
padding: 5px 7px 5px;
border: none;
cursor: pointer;
}
#cash-container{
width: 300px;
background-color: antiquewhite;
color: black;
text-align: center;
border-radius: 10px;
margin-top: 15px;
}
#change-due{
background-color: antiquewhite;
color: black;
}
/* file: script.js */
const cashDrawer = document.getElementById("cash-container");
const result = document.getElementById("change-due");
document.getElementById("purchase-btn").addEventListener("click", () => {
const cash = parseFloat(document.getElementById("cash").value);
let price = 3.26;
const 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 changeResult = checkCashRegister(price, cash, cid);
if (changeResult.status === "OPEN") {
result.innerHTML = `Status: OPEN ${formatChangeResult(changeResult.change)}`;
} else {
result.innerHTML = `<h4>${changeResult.status}</h4>`;
}
});
// Display the available cash in the drawer
const checkCashRegister = (price, cash, cid) => {
const UNIT_AMOUNT = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.10,
"QUARTER": 0.25,
"ONE": 1.00,
"FIVE": 5.00,
"TEN": 10.00,
"TWENTY": 20.00,
"ONE HUNDRED": 100.00
};
let totalCID = 0;
for (let element of cid) {
totalCID += element[1];
}
totalCID = totalCID.toFixed(2);
let changeToGive = cash - price;
const changeArray = [];
if (changeToGive > totalCID) {
return { status:"INSUFFICIENT_FUNDS", change: changeArray };
} else if (changeToGive.toFixed(2) === totalCID) {
return { status: "CLOSED", change: cid };
} else {
cid = cid.reverse();
for (let elem of cid) {
let temp = [elem[0], 0];
while (changeToGive >= UNIT_AMOUNT[elem[0]] && elem[1] > 0) {
temp[1] += UNIT_AMOUNT[elem[0]];
elem[1] -= UNIT_AMOUNT[elem[0]];
changeToGive -= UNIT_AMOUNT[elem[0]];
changeToGive = changeToGive.toFixed(2);
}
if (temp[1] > 0) {
changeArray.push(temp);
}
}
}
if (changeToGive > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
return { status: "OPEN", change: changeArray };
};
// Format the change result
// Format the change result
const formatChangeResult = (changeArray) => {
const formattedChange = changeArray.map(([currency, amount]) => `${currency}: $${parseFloat(amount).toFixed(2)}`).join(" ");
return formattedChange;
};
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register