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 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I assign the contents of an array to variables?

    I'm trying to assign the contents of an array to variables as follows:

    var ay =picsnum[0];
    var by =picsnum[1];
    var cy =picsnum[2];

    all three variables should return an integer which i'm then using to load an image whose location is stored in another array mycontent as follows:

    document.write('<img src="' +mycontent[ay]+'" width="135" height="100" align="top">')

    I know the second part is working cause i'm getting placeholders for the pictures in my browser, but the location of the pictures is undefined.

    Can somebody help me please?

    Thanks a lot
    ilmus

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you sure that the images are loaded in the array?

  • #3
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not the image itself...the path where it is stored I meant:

    mycontent[1]='images/pics/XAR1024A-400.jpg'

  • #4
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm still trying to figure this out. i've tried the pop() method but to no avail.

    Any help pls?

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Can you post all the related codes?

  • #6
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok...here comes the related code:

    <script language="JavaScript">
    var mycontent=new Array()
    //specify random content below.
    mycontent[0]='pic1.jpg'
    mycontent[1]='pic2.jpg'
    mycontent[2]='pic3.jpg'
    mycontent[3]='pic4.jpg'
    mycontent[4]='pic5.jpg'
    mycontent[5]='pic6.jpg'
    mycontent[6]='pic7.jpg'
    mycontent[7]='pic8.jpg'



    var picnums = new array();
    //array with 3 different numbers
    var foundunused = false;
    for (x=0; x<3; x++){
    while (!foundunused ) {
    picnums[x]=Math.floor(Math.random()*mycontent.length);
    foundunused = true;
    for (z=0; z<x; z++){
    if (picnums[x] == picnums[z]) {
    foundunused = false;
    }
    }
    }
    }

    var ay =picsnum[0];
    var by =picsnum[1];
    var cy =picsnum[2];
    </script>

    </head>

    <body>


    <table width="100%" border="0" cellpadding="1" cellspacing="1">
    <tr>
    <td colspan="3" bgcolor="#0A960A">
    <div align="center"><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif"><strong>Xaghra</strong></font></div></td>
    </tr>
    <tr>
    <td width="135" height="100" align="left" valign="top">
    <div align="left">
    <script language="JavaScript">
    <!--

    document.write('<img src=images/pics/tamenzja/' +mycontent[ay]+' width="135" height="100" align="top">')

    //-->
    </script>
    </div></td>


    Thanks

  • #7
    New Coder
    Join Date
    Jan 2005
    Location
    Gozo -Malta
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Problem solved...I used the following code instead of the picnums array and got the desired results:

    var ay, by, cy
    do {
    ay=Math.floor(Math.random()*mycontent.length);
    by=Math.floor(Math.random()*mycontent.length);
    cy=Math.floor(Math.random()*mycontent.length); }
    while ((ay == by) | (ay == cy) | (by == cy)) ;

    Thanks for your help.

    Regards,
    ilmus


  •  

    Posting Permissions

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