I need some help with the project.
Tell me please , why my “cash” variable doesn’t show user input value? When I log it I get an empty object {}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<link rel= "stylesheet" href="styles.css">
<title>register</title>
</head>
<body>
<p id="change-due"></p>
<label for="cash">Money given:</label>
<input id="cash" type="text">
<button type="button" id="purchase-btn">Sell</button>
<div id="cash-in-drawer"></div>
<script src="script.js"></script>
</body>
</html>
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 currencyKey = [
['ONE HUNDRED', 100],
['TWENTY', 20],
['TEN', 10],
['FIVE', 5],
['ONE', 1],
['QUARTER', 0.25],
['DIME', 0.1],
['NICKEL', 0.05],
['PENNY', 0.01],
]
const dic = cid.reverse();
const cash = document.getElementById("cash").value;
const button = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");
const cashInDrawer = document.getElementById("cash-in-drawer");
cashInDrawer.textContent = dic;
const cashMinusPrice = cash.textContent - price;
let change = [];
const sale = (arr) => {
for (let i = 0; i < arr.length; i++) {
if (cashMinusPrice >= arr[i] && dic[i].value - arr[i].value >= 0) {
change.push(dic[i].key);
change.push(Math.floor(dic[i].value / cashMinusPrice));
return result;
}
}
}
const sell = () => {
sale(currencyKey);
if (cash.value < price) {
alert("Customer does not have enough money to purchase the item");
} else if (cash.value === price) {
changeDue.textContent = "No change due - customer paid with exact cash";
}
if (cashInDrawer.value < changeDue.value) {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
}
/*if (cashInDrawer.value === changeDue.value) {
changeDue.textContent = "Status: CLOSED"
}*/
if (cash.value > price) {
changeDue.textContent = `Status: OPEN ${change}`
}
}
button.addEventListener("click", sell);
console.log(change);
console.log(cashMinusPrice);
console.log(cash)