Tell us what’s happening:
I dont know what to do: 12. When
price
is
3.26
, the value in the
#cash
element is
100
,
cid
is
[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]
, and the
#purchase-btn
element is clicked, the value in the
#change-due
element should be
"Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04"
. 13. When
price
is less than the value in the
#cash
element, total cash in drawer
cid
is greater than the change due, individual denomination amounts allows for returning change due, and the
#purchase-btn
element is clicked, the value in the
#change-due
element should be
"Status: OPEN"
with required change due in coins and bills sorted in highest to lowest order. 18. When
price
is
19.5
, the value in the
#cash
element is
20
,
cid
is
[["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
, and the
#purchase-btn
element is clicked, the value in the
#change-due
element should be
"Status: CLOSED PENNY: $0.5"
. 19. When
price
is less than the value in the
#cash
element, total cash in drawer
cid
is equal to change due, and the
#purchase-btn
element is clicked, the value in the
#change-due
element should be
"Status: CLOSED"
with change due in coins and bills sorted in highest to lowest order. // tests completed
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cash Register</title>
</head>
<body>
<h1> Input your cash</h1>
<input 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]
];
let cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");
const demonations = [
["ONE HUNDRED", 100],
["TWENTY", 20],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["DIME", 0.1],
["NICKEL", 0.05],
["PENNY", 0.01]
];
function purchase() {
const cashValue = parseFloat(cash.value);
const changeDueAmount = cashValue - price;
if (cashValue < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
if (changeDueAmount === 0) {
changeDue.innerText = "No change due - customer paid with exact cash";
return;
}
let totalCid = 0;
for (const [denom, amount] of cid) {
totalCid += amount;
}
if (totalCid < changeDueAmount) {
changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
return;
}
let change = [];
let changeRemaining = changeDueAmount;
for(const [denom, value] of demonations) {
let denomAmount = cid.find(([d]) => d === denom)[1];
let amountToReturn = 0;
while (changeRemaining >= value && denomAmount > 0) {
changeRemaining -= value;
denomAmount -= value;
amountToReturn += value;
}
if (amountToReturn > 0) {
change.push(`${denom}: $${amountToReturn.toFixed(2)}`);
}
}
changeRemaining = Math.round(changeRemaining * 100) / 100;
if (changeRemaining > 0) {
changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
} else if (changeRemaining === 0 && totalCid === changeDueAmount) {
changeDue.innerText = `Status: CLOSED ${change.join(" ")}`;
}
else {
changeDue.innerText = `Status: OPEN ${change.join(" ")}`;
}
}
purchaseBtn.addEventListener("click", purchase);
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register