Write a JavaScript program to make change. You should use prompt() to read a str
ID: 3782313 • Letter: W
Question
Write a JavaScript program to make change. You should use prompt() to read a string and parseInt() to convert the string into an integer number of cents (i.e. fractions of a dollar). Your program should write to the document how to deliver that number of cents as change using a minimum number of US coins as output. Ignore the 50 cent piece as a possible coin. So for example, given an input of 87, your program should output the following to the document:
87 cents is comprised of:
3 quarter(s)
1 dime(s)
0 nickle(s)
2 pennies
Hint: Remainders from division are calculated with the modulus operator.
Explanation / Answer
function addFields(form) {
var strCents = prompt("Number of cents: ", "Type number here");
var cents = parseInt(strCents);
var quarters = Math.floor(cents / 25);
var dimes = Math.floor((cents % 25) / 10);
var nickels = Math.floor(((cents % 25) % 10) / 5);
var pennies = cents % 5;
alert(cents + " cents is comprised of:
" + quarters + " quarter(s)
" + dimes + " dime(s)
" + nickels + " nickel(s)
" + pennies + " pennies
");
}
if you like this answer, please give a thumbs up and if you have some doubt just ask in the comment section below. I will try to help. Cheers
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.