www.webdeveloper.com
Results 1 to 4 of 4

Thread: Creating new variables

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    27

    Post Creating new variables

    Hi all,

    I have the following code which I need some help with.

    Code:
    <script>
    var images = [
    	"",  /* PICTURE 0 */
    	"",  /* PICTURE 1 */
    	"",  /* PICTURE 2 */
    	"",  /* PICTURE 3 */
    	
     ];
    </script>
    <body>
    <div id="chooseframe">
    
    </div>
    <script>
    var holding = document.getElementById("chooseframe");
    var imageholders = [];
    for(var i=0;i==images.length;i++)
    {
    imageholders.push("number"+i);
    imageholders[i] = document.createElement("IMG");
    imageholders[i].src=images[i];
    holding.appendChild(imageholders[i]);
    }
    
    </script>
    </body>
    This is meant to be a code snippet in which the images inside the "images" variable are displayed in a list (to control which image is being displayed on a slideshow)

    The problem lies with the fact that I have never used "create node" DOM before and therefore don't know whats wrong here. I have a feeling its that the loop is trying to put the new image nodes in the same place (not 1 then another then another like a list). The problem with this line of thought, however, is that I then don't know how to do this in another way if I am correct.

    Any and all help with be great.

    Thanks all

    ps: dumb question, how do you edit a post on this site? I can't for the life of me find the big "edit" button.

  2. #2
    Join Date
    Jul 2013
    Posts
    66
    There is one major and one minor issue with your code. The minor issue is that the push call to imageholders is completely pointless as you will override that element in the next line anyway. The major issue is your loop condition:

    Code:
    for(var i=0;i==images.length;i++)
    It should probably be

    Code:
    for(var i=0;i<images.length;i++)
    This will at least display the pictures. As for the slideshow you're talking about I'm not sure what exactly it is you want. Currently it will just display all images in the images array.

    P.S.: I think you can't edit a post unless you have a certain number of posts.

  3. #3
    Join Date
    Jul 2013
    Posts
    27

    Perfect!

    Quote Originally Posted by Airblader View Post
    There is one major and one minor issue with your code. The minor issue is that the push call to imageholders is completely pointless as you will override that element in the next line anyway. The major issue is your loop condition:

    Code:
    for(var i=0;i==images.length;i++)
    It should probably be

    Code:
    for(var i=0;i<images.length;i++)
    This will at least display the pictures. As for the slideshow you're talking about I'm not sure what exactly it is you want. Currently it will just display all images in the images array.

    P.S.: I think you can't edit a post unless you have a certain number of posts.
    Works brilliantly now that I implemented your two fixes.

    Minor error: Yet another dumb moment when I thought I had to create the variable before I could assign in a value.

    Major error: Still not even sure why that didn't work. I thought the For loops second part was the condition at which it ends the loop (ie a given point or a greater or lesser then point). O well anyway, gap in my knowledge filled in.

    The slideshow i mentioned is already working with another code which I didn't post. Sorry if this confused you :S

    Thanks bud.

  4. #4
    Join Date
    Jul 2013
    Posts
    66
    I thought the For loops second part was the condition at which it ends the loop
    You thought wrong! You better read up on it here and make sure to understand the difference, because it is crucial.

    But FYI, even if it were true, 'i == images.length' would still be wrong. Your array has a length of 4, which means the indices range goes from 0 to 3. If i were to be 4, you would run out of bounds. Granted, in Javascript it will only return 'undefined', in other languages your program will die with an error.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles