HopefulCoder

07-10-2011, 12: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.

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.