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

#1 B0urn3  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 21
  • Joined: 24-February 13

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 what I have so far:

var $ = function (id) {
    return document.getElementById(id);
}

var make_change = function() {
		var cents = parseInt($("cents").value);
		
		quarters = 25;
		dimes = 10;
		nickels = 5;
		pennies = 1;
		
	   var remainder = cents / quarters;
	       remainder = parseInt(remainder.toFixed(1));
	       totalDimes =(cents % quarters) / dimes;
	       totalDimes = parseInt(totalDimes.toFixed(1));
	       totalNickels = (cents % quarters) % dimes / nickels;
	       totalNickels = parseInt(totalNickels.toFixed(1));
	       totalPennies = totalNickels % pennies;
	       totalPennies = parseInt(totalPennies.toFixed(1));
	       
		
		
		$("quarters").value = remainder;
		$("dimes").value = totalDimes;
		$("nickels").value = totalNickels;
		$("pennies").value =totalPennies;
		
		
		
		
		
		
			
}			

window.onload = function () {
   $("calculate").onclick = make_change;
   
} 


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.

Is This A Good Question/Topic? 3
  • +

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

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,581
  • Joined: 12-December 12

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

Posted 05 September 2013 - 07:19 PM

A good first post, but all your variables should be declared with var. This isn't necessary if more than one is declared in the same statement:

var thisOne, thatOne;

Suppose there were 89 cents.

var quarters = 89 / 25;    // 3.56
var quartersWhole = parseInt(quarters);    // 3
var remainder = 89 - quartersWhole * 25;    // 14


You can make use of the modulus within this code if you want, but this should enable you to work out what is needed.

toFixed() is only intended for displaying a number (as a string).

This post has been edited by andrewsw: 05 September 2013 - 07:49 PM

Was This Post Helpful? 3
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

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.
Was This Post Helpful? 4
  • +
  • -

#4 B0urn3  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 21
  • Joined: 24-February 13

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

Posted 06 September 2013 - 08:39 AM

Good morning and I am grateful for your feedback.

Okay, so I changed my totalPennies variable to
totalPennies = cents % nickels



Saying that....every single value the user enters 0 - 99 calculates correctly....

Expect...

values 24 and values 99.

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:

cents / 24 = 0.96 which parseInt(remainder.toFixed(1)); supposedly returns 0, right?
and
cents / 99 = 3.96 which parseInt(remainder.toFixed(1)); supposedly returns 3, right?

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 user enters 99 we get quarters = 4, dimes = 2, nickels = 0, and pennies = 4. Well....4 quarters = $1.00 + .24 = (1.24)

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.

Once again,
Thank you,
Quintius

New code:


var $ = function (id) {
    return document.getElementById(id);
}

var make_change = function() {
		var cents = parseInt($("cents").value);
		
		var quarters = 25;
		var dimes = 10;
		var nickels = 5;
		var pennies = 1;
		
	   var remainder = cents / quarters;
	       remainder = parseInt(remainder.toFixed(1));
	       totalDimes =(cents % quarters) / dimes;
	       totalDimes = parseInt(totalDimes.toFixed(1));
	       totalNickels = (cents % quarters) % dimes / nickels;
	       totalNickels = parseInt(totalNickels.toFixed(1));
	       totalPennies = (cents % nickels);
	       totalPennies = parseInt(totalPennies.toFixed(1));
	       
		
		
		$("quarters").value = remainder;
		$("dimes").value = totalDimes;
		$("nickels").value = totalNickels;
		$("pennies").value =totalPennies;
		
		
		
		
		
		
			
}			

window.onload = function () {
   $("calculate").onclick = make_change;
   
}

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,603
  • Joined: 08-June 10

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

Posted 06 September 2013 - 09:04 AM

View PostB0urn3, on 06 September 2013 - 05:39 PM, said:

cents / 24 = 0.96 which parseInt(remainder.toFixed(1)); supposedly returns 0, right?
and
cents / 99 = 3.96 which parseInt(remainder.toFixed(1)); supposedly returns 3, right?

nope. let print remainder.toFixed(1). you should get 1.0 and 4.0 because toFixed() rounds the values correctly before converting to string.
Was This Post Helpful? 1
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

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

Posted 06 September 2013 - 11:41 AM

View PostB0urn3, on 06 September 2013 - 03:39 PM, said:

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.

View PostB0urn3, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1