$("fiveCents").value = fiveCents;
$("tenCents").value = tenCents;
$("twentyCents").value = twentyCents;
$("fiftyCents").value = fiftyCents;
These variable are not declared anywhere
Each condition in the
function calculateMinChange(amount) {
returns a variable as above which is not declared.
and the amount calculation is pointless, since you are not using it anywhere, so i thing you are trying to return the amount. you only will know the logic .
Correct the above, it should work.
Updated Solution
<html>
<head>
<script>
var $ = function (id) {
return document.getElementById(id);
}
const fiveChange = 5;
const tenChange = 10;
const twentyChange = 20;
const fortyChange = 40;
const fiftyChange = 50;
function calculateMinChange(amount) {
if (amount >= fiftyChange) {
$("fiftyCents").value = 1;
amount = amount - 50;
return amount;
}
else if (amount >= fortyChange) {
$("twentyCents").value = 2;
amount = amount - 40;
return amount;
}
else if (amount >= twentyChange) {
$("twentyCents").value = 1;
amount = amount - 20;
return amount;
}
else if (amount >= tenChange) {
$("tenCents").value = 1;
amount = amount - 10;
return amount;
}
else (amount >= fiveChange)
{
$("fiveCents").value = 1;
amount = amount - 5;
return fiveCents;
}
}
function processEntries() {
$("fiveCents").value = 0;
$("tenCents").value = 0;
$("twentyCents").value = 0;
$("fiftyCents").value = 0;
var amount = parseInt($("amount").value);
if (isNaN(amount) || amount > 99) {
alert("Change must be numeric and under 99")
}
else {
debugger
while (amount >= 5)
amount = calculateMinChange(amount);
}}
window.onload = function () {
$("calculate").onclick = processEntries;
$("amount").focus();
}
</script>
</head>
<body>
Change Calculator
<label for="amount">Enter number of cents (0-99).</label>
<input type="text" id="amount">
<label> </label>
<input type="button" id="calculate" value="calculate">
<label for="fiveCents">Five cents: </label>
<input type="text" id="fiveCents">
<label for="tenCents">Ten cents: </label>
<input type="text" id="tenCents">
<label for="twentyCents">Twenty Cents: </label>
<input type="text" id="twentyCents">
<label for="fiftyCents">Fifty Cents: </label>
<input type="text" id="fiftyCents">
</body>
</html>