Help! Creating a javascript program that makes change and having issue

Hello! I am having an issue with the modulus operator in my program. This program needs to show the conversion of change into quarters, dimes, nickels and pennies. I can not get it to round and I think I have the modulus operator set up correctly. Can someone please lead me in the right direction.

Thank you!









var numberStr;
var numberInt;
var quarterNumber;
var remainder;

numberStr=prompt("Please enter a number in cents that you would like me to show the number of quarters, dimes, nickels and pennies that make up that number:","1");
numberInt=parseInt(numberStr);
quarterNumber=(numberInt/25);
quarterRemainder=(numberInt%25);
dimeNumber=(quarterRemainder/10);
dimeRemainder=(quarterRemainder%10);
nickelNumber=(dimeRemainder/5);
nickelRemainder=(dimeRemainder%5);
pennyNumber=(nickelRemainder/1);
pennyRemainder=(nickelRemainder%1);
document.write("You have entered " + numberInt + " cents. Here is a breakdown..." + "
" + "
");
document.write("Quarters: " + quarterNumber + "
");
document.write("Dimes: " + dimeNumber + "
");
document.write("Nickels: " + nickelNumber + "
");
document.write("Pennies: " + pennyNumber + "
");




Comments

  • Use Math.floor.

    [code]








    var numberStr;
    var numberInt;
    var quarterNumber;
    var remainder;

    numberStr=prompt("Please enter a number in cents that you would like me to show the number of quarters, dimes, nickels and pennies that make up that number:","1");
    numberInt=parseInt(numberStr);
    quarterNumber=Math.floor(numberInt/25);
    quarterRemainder=(numberInt%25);
    dimeNumber=Math.floor(quarterRemainder/10);
    dimeRemainder=(quarterRemainder%10);
    nickelNumber=Math.floor(dimeRemainder/5);
    nickelRemainder=(dimeRemainder%5);
    pennyNumber=Math.floor(nickelRemainder/1);
    pennyRemainder=(nickelRemainder%1);
    document.write("You have entered " + numberInt + " cents. Here is a breakdown..." + "
    " + "
    ");
    document.write("Quarters: " + quarterNumber + "
    ");
    document.write("Dimes: " + dimeNumber + "
    ");
    document.write("Nickels: " + nickelNumber + "
    ");
    document.write("Pennies: " + pennyNumber + "
    ");




    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion