I am writing a piece of code, when a user enters the amount of change owed, returns the minimum amount of coins to equal that change. For instance, assuming only quarters, dimes, nickels and pennies are used. You enter $1.00. The number my code will render on the screen is 4 (4 quarters equaling a dollar). However, I am experiencing a small bug. If you type in $0.41 for example, you'd expect the code to render 4 again (1 quarter + 1 dime + 1 nickel + 1 penny is the minimum amount of permutations to equal 41 cents), but it doesn't, it renders 5.44. Please help! Thank you in advance.
document.write("Hi,how much change is due? ");
function greed () {
var n = document.getElementById('change').value;
if (n >=0)
{
function amount (amt){
return amt/25 + (amt%25)/10 + ((amt%25)%10)/5 + ((amt%25)%10)%5;
}
document.write(amount(Math.round(n*100)));
}
else {alert("Invalid Amount!")};
}
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Greedy! </title>
<script type="text/javascript" src="greedy.js" ></script>
</head>
<body>
<form>
<fieldset>
<input type="number" id="change" name="change" value="0"/>
<input type="submit" id="submit" name="submit" value="submit" onclick="greed();"/>
</fieldset>
</form>
</body>
</html>