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
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help on Random Number function

    New to Javascript. Why am I getting an additional undefined after the double random?

    [CODE]
    function PlayGame(){

    var min = 0;
    var max = 20;

    var randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
    var doubleRandom = (randomNumber * 2);

    document.write(randomNumber);
    document.write(doubleRandom);

    }
    [CODE]

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    343
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Works fine here, in IE9, Chrome and FF:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Demo</title>
    <script>
    function PlayGame(){
    
         var min = 0;
         var max = 20;
    
         var randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
         var doubleRandom = (randomNumber * 2);
    
         document.write(randomNumber);
         document.write('<br>');
         document.write(doubleRandom);
    
    }
    </script>
    </head>
    <body onload="PlayGame()">
    
    </body>
    </html>
    Frank

    How to: Target IE in, Position in, Center in, Create a Fixed ('Sticky') Footer with, and Create a Drop-Down/Fly-Out Menu with CSS: Website Laten Maken Amsterdam.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Posted in wrong forum. Has nothing to do with DOM or JSON.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,919
    Thanks
    6
    Thanked 1,040 Times in 1,013 Posts
    Next time, Old Pedant, hit the “report” button so that we moderators get an e-mail with that note. I just came across this by accident, I’m not always browsing every sub forum for wrongly posted threads.


  •  

    Posting Permissions

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