5 Replies - 3550 Views - Last Post: 06 September 2013 - 11:41 AM

How do you use modulus to get the value of pennies

Posted 05 September 2013 - 06:27 PM

Hello everyone. I'm new to the forum so please forgive my violations where they occur. (They're not intentional.) Also, for the most part I'm also new to programming. I began my junior year of computer programming this fall which entails PHP, JAVA, and Javascript/JQuery.(The only programming language that we've covered thus far is Visual Basic). This question is about Javascript.

I have an assignment due tomorrow night that calls for the student to develop an application that tells how many quarters, dimes, nickels, and pennies are needed to make change for any amount of change from 0 - 99 cents. As a hint it says: One way to get the results is to use the divide and modulus operators along with the parseInt method for truncation the results so they are whole numbers.

Here's my problem. (Well for starters). If the user enters values above 25, in multiples of 5, i.e, 25, 30, 35, 40, etc. the code returns the correct change. However, this is not the case if user enters say, 26, or 31, or 58, etc. I'm theorizing that I should be able to use modulus to get my value for pennies as I did here:

totalPennies = totalNickels % pennies;

But that doesn't cut it for integers that fall within 0 and 5. (1,2,3,4) nor integers between 5 and 10. I'm stomped here and I believe that I don't have enough programming knowledge under my belt to be able to express the math needed to be done on the pennies variable.
This is only my third Javascript application and the first two dealt with IF statements/Loops.

Also, since I haven't yet dealt with the part of the application that handles values less than 25, my question is...do I put those statements inside an IF statement? If so, is it possible to place an IF statement inside of the code that runs within the function

var make_change = function()

.

Lastly as previously stated, I sincerely apologize if I violated any forum rules.
Thanks.

Re: How do you use modulus to get the value of pennies

Posted 05 September 2013 - 07:56 PM

I wouldn't recommend using parseInt to round the numbers down to whole integers. That function is meant to convert numeric strings to integers, not to manipulate actual numbers. When you use it like you do, the code is actually converting the number to a string first, and then parsing it back into an integer. Hardly ideal. - I would suggest using the Math.floor function instead. That one is actually meant to deal with numbers, and it achieves the exact same result as parseInt does, just in a more "natural" way.

As for the question as a whole. I don't know how far you've gone so far in your studies, so this may not have been introduced to you yet, but in this situation you could streamline this code a great deal by using arrays/objects and loops. You have a list of coins that you need to go through to calculate the change, so a loop would be the ideal way to solve that.

Consider if you stored your coin sizes in an object like this. This is a basic object declaration, where an object is created and a few properties added to it:

var coinSizes = {
quarter: 25,
dime: 10,
nickel: 5,
penny: 1
};

Now you can use the "for...in" version of the for loop to go through the properties assigned to the object and do some calculations for each property. These calculations are done exactly like andrewsw suggested, but without having to repeat those lines for each coin type. It creates a new object for the results, and adds the results of the calculations to a new property of that result object, matching the current property of the coinSizes object.

// Define a new "result" object to store the results of
// the calculations.
var results = {};
// Go through each coin size. The "coin" variable here will
// contain the key from the coinSizes object, which is the
// name of the coin. You can then use that inside the for
// loop to access the coinSize value for that coin.
for (var coin in coinSizes) {
// Calculate how many of the current coin exists in the
// "pennies" value. Then save that in a new element of the
// "results" object using the name of the coin.
results[coin] = Math.floor(pennies / coinSizes[coin]);
// Remove the total pennies from the "pennies" total for the
// coin calculated above. That will ensure that the calculations
// for the next coin are done as if the total for this coin has
// be accounted for already.
pennies -= results[coin] * coinSizes[coin];
}
// Now the results object holds the correct value for each coin.
// So if you had 89 as the pennies value, this would be true:
console.log(results.quarter); // 3
console.log(results.dime); // 1
console.log(results.nickel); // 0
console.log(results.penny); // 4

As you can see, now all the calculations, regardless of how many coin types you may have, are done using only 4 lines of code.

I've spent my entire morning entering values into the application. (If this is what debugging consist of, either it would tremendously suck to have that job or there has to be an easier way to debug that I haven't learned yet.) But this is what happens with those values:

Well, that doesn't happen in the case of ".96". As we can see, in these special instances we get the values 1 and 4.

Researching the problem I think I got ahead of myself and as a result....confused myself. I'm thinking this is what was meant by the suggestion that I use math.floor and math.ceiling instead of

parseInt(varible.toFixed(1));

I don't know how those work yet so I decided against trying them out in my code. Furthermore, numbers are weird like that sometimes. Ironically, the values if user enters 24 come out to quarters = 1, dimes = 2, nickels = 0, and pennies = 4. (124).

If our quarters value isn't incremented by 1 in either case, the 2 dimes and 4 pennies values are the equal both 24 and 99. I don't know if this is coincidence, number theory, etc? All I know is that it's interesting and beyond my coding ability to fix.

@Atli, I'm vaguely familiar with arrays. (only what I picked up in CodeAcademy). Thus, I somewhat follow what you're saying I'm just confused on where I use var cents and my variables for coins in your proposed code.

Re: How do you use modulus to get the value of pennies

I'm thinking this is what was meant by the suggestion that I use math.floor and math.ceiling instead of

parseInt(varible.toFixed(1));

I don't know how those work yet so I decided against trying them out in my code.

There isn't much to know, really. Math.floor rounds numbers down to the closest integer, and Math.ceil rounds them up to the closest integer. That's it.

The rules surrounding converting numbers to and from strings are far more confusing than the rules for rounding numbers. What Dormilich pointed out demonstrates one aspect of that.

B0urn3, on 06 September 2013 - 03:39 PM, said:

@Atli, I'm vaguely familiar with arrays. (only what I picked up in CodeAcademy). Thus, I somewhat follow what you're saying I'm just confused on where I use var cents and my variables for coins in your proposed code.

The cents variable was the total number of cents fetched from the user input, right? In my loop example I called that variable pennies. (Not sure what the difference between those words is, so forgive me if I'm using them incorrectly.)

As for your coin variables. You wouldn't need them. The results object would store all the results. That would then be used to update the UI with the outcome of the calculations.

Don't worry if it doesn't make sense yet. Once you've studied objects and loops it will. What you should take away from what I an andrewsw posted is the way we calculated the results. Whether you use loops or not, that is what you need to be implementing in your code.