Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Rounding Problem

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...
Code:
```<html>
<title></title>
<script type="text/javascript">
function eggs(number)
{
var total=prompt("eggs","");
var dozen=(total/12);
var gross=(total/144);
var fulldozen=(Math.round(dozen));
var fullgross=(Math.round(gross));
}
</script>
<body>
<form>
<a href="javascript:eggs('number')"><input type="button" value="Eggs" name="Eggs" size="30"></a>
</form>
</body>
</html>```

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.

• ## Something to try ...

See if this works for you ...
Code:
```<html>
<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';
}
</script>
<body>
<form>
<a href="javascript:void(0)" onclick="eggs('number')">
<input type="button" value="Eggs" name="Eggs" size="30"></a>
</form>
</body>
</html>```

• ## Users who have thanked jmrker for this post:

HopefulCoder (07-10-2011)

• Hi

Which methods? round either up or down?
see Math.floor() vs Math.round() HERE

LT

• ## Users who have thanked low tech for this post:

HopefulCoder (07-10-2011)

• 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.

Code:
```<html>
<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>
<body>
<form>
<a href="javascript:eggs('number')"><input type="button" value="Eggs" name="Eggs" size="30"></a>
</form>
</body>
</html>```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•