Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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>
    <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.
    Last edited by HopefulCoder; 07-10-2011 at 01:15 AM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,077
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Something to try ...

    See if this works for you ...
    Code:
    <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>

  • Users who have thanked jmrker for this post:

    HopefulCoder (07-10-2011)

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    851
    Thanks
    172
    Thanked 93 Times in 93 Posts
    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)

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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>
    <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>


  •  

    Posting Permissions

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