While loop...

Discussion in 'JavaScript' started by pixelpie, Dec 2, 2007.

  1. #1
    Hi, using Math.random(), I need to show one of six pictures every time I reload the page. I need to use the while loop too.

    So far I have this, but it only shows four of the six pictures I have.

    <script language="JavaScript">
    
    var random = Math.random() ;
    var picnum = Math.round(random*4)+1;
    
    </script>
    </head>
    
    <script language="JavaScript">
    
    document.write('<img src=picture/film'+picnum+'.jpg>')
    
    
    
    </script>
    HTML:
    Thanks for your help! :eek:
     
    pixelpie, Dec 2, 2007 IP
  2. hrcerqueira

    hrcerqueira Peon

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this:
    
    var picnum = parseInt(Math.random() * 6) + 1;
    
    Code (markup):
     
    hrcerqueira, Dec 2, 2007 IP
  3. pixelpie

    pixelpie Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Doesn't seem to work but thanks for your help anyway!
     
    pixelpie, Dec 3, 2007 IP
  4. hrcerqueira

    hrcerqueira Peon

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Of course it works... Unless you're using a very old browser it works flawlessly...

    create a html page with just this in it:

    
    <html>
    <head></head>
    <body>
    <script>
    var picnum = parseInt(Math.random() * 6) + 1;
    alert(picnum);
    </script>
    </body>
    </html>
    
    Code (markup):
    then open it, and refresh it many times... you'll always get a number between 1 an 6.

    If it still doesn't work for you, consider enabling javascript on your browser...
     
    hrcerqueira, Dec 3, 2007 IP