...

View Full Version : Rounding Problem



HopefulCoder
07-10-2011, 01:55 AM
I recently started trying to learn javascript and I decided to try an exercise I found somewhere. Basically it involves counting eggs into total, dozen, and gross. The problem is when I put in a number lower than a full dozen or gross it rounds up and says I have the full dozen intead of the fraction.

Ex. I say I have 143 eggs, it says I have 12 dozen. Obviously I am 1 short. Apparently the method(I think that is what it is called?) rounds to the nearest number. Which methods? round either up or down?

Here is the code...


<html>
<head>
<title></title>
<script type="text/javascript">
function eggs(number)
{
var total=prompt("eggs","");
alert("You have "+total+" eggs.");
var dozen=(total/12);
alert("You have "+dozen+" dozen eggs.");
var gross=(total/144);
alert("You have "+gross+" gross eggs.");
var fulldozen=(Math.round(dozen));
alert("You have "+fulldozen+" dozen eggs.");
var fullgross=(Math.round(gross));
alert("You have "+fullgross+" gross eggs.");
}
</script>
</head>
<body>
<form>
<a href="javascript:eggs('number')"><input type="button" value="Eggs" name="Eggs" size="30"></a>
</form>
</body>
</html>


Thanks for reading.

Edit: Also, it goes through the whole thing twice. It will prompt the first time, go through, then prompt again. It terminates after the second though.

jmrker
07-10-2011, 04:36 AM
See if this works for you ...


<html>
<head>
<title></title>
<script type="text/javascript">
function eggs(number) {
var str = '';
var total=prompt("eggs","");
str += "You have "+total+" eggs.";
var dozen=parseInt(total/12); var remain = total % 12;
str += "\nYou have "+dozen+" dozen eggs and "+remain+' left over';
var gross=parseInt(total/144); var remain = total % 144
str += "\nYou have "+gross+" gross eggs and "+remain+' left over';
alert(str);
}
</script>
</head>
<body>
<form>
<a href="javascript:void(0)" onclick="eggs('number')">
<input type="button" value="Eggs" name="Eggs" size="30"></a>
</form>
</body>
</html>

low tech
07-10-2011, 05:33 AM
Hi



Which methods? round either up or down?

see Math.floor() vs Math.round() HERE (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math)

LT

HopefulCoder
07-10-2011, 07:53 AM
Awesome! Thank you both so much. I was able to configure the code thanks to low tech. Although my code is not as elegant as yours, jmrker. There are some parts of that code that I have not learned yet so I will be dissecting that some tomorrow. Thanks.

Any ways, here is my final code.



<html>
<head>
<title>Egg Counting</title>
<script type="text/javascript">
function eggs(number)
{
var total=prompt("eggs","");
//calculates gross
var g1=(total/144);
var g2=Math.floor(g1);
var g3=(g1-g2);
var g4=(g3*144)
//calculates dozens and remainder
var da=(g4/12);
var db=Math.floor(da);
var dc=(da-db);
var dd=(dc*12)
alert("You have "+g2+" gross "+db+" dozen "+dd+" eggs left over.");
}
</script>
</head>
<body>
<form>
<a href="javascript:eggs('number')"><input type="button" value="Eggs" name="Eggs" size="30"></a>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum