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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript: multiple values from array

    I have the following script that currently returns 4 of the same images. I can't wrap my head around how to get it to return 4 random images (out of the 7 in this example)? Also the links aren't working?

    ---
    Code:
    var imagenumber = 7 ;
    var randomnumber = Math.random() ;
    var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1;
    
    images= new Array
    images[1] = "image1.jpg"
    images[2] = "image2.jpg"
    images[3] = "image3.jpg"
    images[4] = "image4.jpg"
    images[5] = "image5.jpg"
    images[6] = "image6.jpg"
    images[7] = "image7.jpg"
    var image = images[rand1]
    
    links= new Array
    links[1] = "link1.html"
    links[2] = "link2.html"
    links[3] = "link3.html"
    links[4] = "link4.html"
    links[5] = "link5.html"
    links[6] = "link6.html"
    links[7] = "link7.html"
    var link = links[rand1]
    
    document.write('<div id="image-1"><a href="' + link + '"><img src="' + image + '"></a></div><div id="image-2"><a href="' + link + '"><img src="' + image + '"></a></div><div id="image-3"><a href="' + link + '"><img src="' + image + '" border="0"></a></div><div id="image-4"><a href="' + link + '"><img src="' + image + '"></a></div>')
    ---
    Any help would be greatly appreciated!

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Code:
    images= []
    images[1] = "image1.jpg"
    images[2] = "image2.jpg"
    images[3] = "image3.jpg"
    images[4] = "image4.jpg"
    images[5] = "image5.jpg"
    images[6] = "image6.jpg"
    images[7] = "image7.jpg"
    
    var rand1 = Math.floor(Math.random()*images.length))
    
    var image = images[rand1]

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks Blaze - I REALLY appreciate the quick reply

    Your response is maybe too brief for me - Javascript is not exactly my strong suit. Can you look at my initial code and tell me if I have anything unnecessary or otherwise holding me back from what I need to accomplish?

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    oh, I misunderstood...

    Code:
    function getImage(){
    var imagenumber = 7 ;
    var randomnumber = Math.random() ;
    var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1;
    
    images= new Array
    images[1] = "image1.jpg"
    images[2] = "image2.jpg"
    images[3] = "image3.jpg"
    images[4] = "image4.jpg"
    images[5] = "image5.jpg"
    images[6] = "image6.jpg"
    images[7] = "image7.jpg"
    var image = images[rand1]
    
    links= new Array
    links[1] = "link1.html"
    links[2] = "link2.html"
    links[3] = "link3.html"
    links[4] = "link4.html"
    links[5] = "link5.html"
    links[6] = "link6.html"
    links[7] = "link7.html"
    var link = links[rand1
    }
    
    for (i=0; i<4 ; i++){
    getImage();
    document.write('<div id="image-1"><a href="' + link + '"><img src="' + image + '"></a></div><div id="image-2"><a href="' + link + '"><img src="' + image + '"></a></div><div id="image-3"><a href="' + link + '"><img src="' + image + '" border="0"></a></div><div id="image-4"><a href="' + link + '"><img src="' + image + '"></a></div>')
    }
    I thought you were having trouble with the random number generation...

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    yeah, you needed you reset the random number generation each time. then create a loop writing your image with each random number.

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This is really close to what I was working on about an hour ago - for (i=0; i<4 ; i++){

    But it's still not bringing up the four random images/links. Not sure if it's in the document.write statement or what - something is amiss. I get four of the same image with the original code I posted. With this code nothing appears at all?

    All the syntax checks-out (I added ']' to var link = links[rand1]).

    Any idea of the document.write block is hosed or something else maybe? Thanks again, I know why you go by 'blaze' - very fast

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    actually I just encapsulated your entire code in one function, so technically you would get 16 img's
    Code:
    images = [ "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg", "image6.jpg", "image7.jpg"] //Setup your array
    
    links =  [ "link1.html", "link2.html", "link3.html", "link4.html", "link5.html", "link6.html", "link7.html"]//Setup your array
    
    function getImage(){
    var rand1 = Math.floor(Math.random()*images.length)  //This sets your max number to the length of your array, so you don't have to change that var every time you add to the array (just change the array!)
    link = links[rand1]    // I removed the "var" here because that prevents you from accessing it outside of getImage()
    image = images[rand1]    // I removed the "var" here because that prevents you from accessing it outside of getImage()
    }
    
    for (i=0; i<4 ; i++){
    getImage()
    document.write('<div id="image-' + i + '"><a href="' + link + '"><img src="' + image + '"></a></div>')
    }
    Also, typo in my last response is fixed here

  • Users who have thanked blaze4218 for this post:

    Sha66y (09-23-2011)

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I know this is going to mess things up a little, but can I point out that arrays start at 0, making the first object of those arrays undefined.

    which may not be bad, but it can't be good...

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Quote Originally Posted by Sha66y View Post
    I know why you go by 'blaze' - very fast
    nickname from when I was a kid actually, total pyro --TMI?
    I actually have another pen-name for video games for when I use a *fast* character class "Blitz"

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    @xelawho mine? how so? the "white space" is ignored by javascripts parser

  • #11
    New Coder
    Join Date
    Aug 2009
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Blaze - you are brilliant! Thank you! All four images showing up now randomly. I get some dupes out of the 4 - something I didn't even ponder.

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    You can prevent that

  • #13
    New Coder
    Join Date
    Aug 2009
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by blaze4218 View Post
    nickname from when I was a kid actually, total pyro --TMI?
    I actually have another pen-name for video games for when I use a *fast* character class "Blitz"
    I know a guy nicked-named Gas because he drank gas as a kid - true story. You and him maybe shouldn't meet

  • #14
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    no, you were too fast for me, too this was back when you were assigning positions to objects: images[1] = "image1.jpg"

    etc

  • #15
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    oh, lol, yeah that was just cut and paste of his code
    Last edited by blaze4218; 09-23-2011 at 08:02 PM. Reason: spellcheck


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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