Tell us what’s happening:
I don’t know why the Fourth Condition doesn’t get checked, the alert shows up when the “#cash” element is less than the price !
Your code so far
<!-- file: index.html -->
<input type ="number" id="cash"></input>
<div id="change-due"></div>
<button id="purchase-btn">Purchase</button>
<script>
document.querySelector('#purchase-btn').addEventListener('click', function() {
let cash = parseFloat(document.querySelector('#cash').value);
let price = 3.87;
let cid = [
["PENNY", 0.5],
["NICKEL", 1],
["DIME", 5],
["QUARTER", 10],
["ONE", 14],
["FIVE", 55],
["TEN", 50],
["TWENTY", 60],
["ONE HUNDRED", 100]
];
const change = calculateChange(price, cash, cid);
document.getElementById('change-due').innerText = change;
});
function calculateChange(price, cash, cid) {
const currencyValues = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100
};
const totalCashInDrawer = cid.reduce((acc, curr) => acc + curr[1], 0);
let changeDue = cash - price;
if (changeDue < 0) {
alert("Customer does not have enough money to purchase the item");
return ;
}
}
</script>
/* file: styles.css */
/* file: script.js */
//document.getElementById('')
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