Tell us what’s happening:
My event listener isn’t producing the alert or inner text on the Building a cash register cert. Nothing happens and there’s no errors in the console. I’m not sure why. Code is below.
purchase.addEventListener(“click”, () => {
const cashValue = parseFloat(cash.value);
const changeDue = cashValue - price;
if (cashValue < price) {
alert(“Customer does not have enough money to purchase the item”);
return;
}
if (cashValue === price) {
change.innerText = “No change due - customer paid with exact cash”;
}
});
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>
<main>
<h1>Cash Register</h1>
<input id="cash">
<div id="change-due"></div>
<button id="purchase-btn">Purchase</button>
</main>
<script src="./script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
let price = 1.87;
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]
];
const cash = document.getElementById("cash");
const change = document.getElementById("change-due");
const purchase = document.getElementById("purchase-btn");
let currencyUnits = [
['PENNY', .01],
['NICKEL', .05],
['DIME', .1],
['QUARTER', .25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE HUNDRED', 100]
];
purchase.addEventListener("click", () => {
const cashValue = parseFloat(cash.value);
const changeDue = cashValue - price;
if (cashValue < price) {
alert("Customer does not have enough money to purchase the item");
return;
}
if (cashValue === price) {
change.innerText = "No change due - customer paid with exact cash";
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register