www.webdeveloper.com
Results 1 to 6 of 6

Thread: just a one "for"

  1. #1
    Join Date
    Nov 2002
    Posts
    3

    Post just a one "for"

    Hi,

    in my JavaScript i need the following lines ...

    pic1.src=bild[num].src;
    pic2.src=bild[num+1].src;
    pic3.src=bild[num+2].src;
    pic4.src=bild[num+3].src;
    pic5.src=bild[num+4].src;
    pic6.src=bild[num+5].src;
    pic7.src=bild[num+6].src;

    Can i somehow use "for" to shorten it, like e.g.

    anzahl=7
    for (var i=1;i<=anzahl;i++) { pic1.src=bild[num+(i-1)].src; }

    the problem is, i also need to replace the "1" in the
    pic1, pic2, pic3 ... part

    How can I do this ?

    cu
    Philip

  2. #2
    Join Date
    Nov 2002
    Location
    London UK
    Posts
    154
    Ok, I don't know what you want to do this for so I can only help so much!!

    <script language="javascript">
    var i;
    var newarray = new Array()

    for (i = 1; i <= 7; i++)
    {
    if (i == 1)
    {
    newarray[i] = "pic" + i + ".src=bild[num].src;"
    }
    else
    {
    newarray[i] = "pic" + i + ".src=bild[num+" + (i - 1) + "].src;"
    }
    }
    </script>

    Ok this formulates those lines you want into strings. The reason the for loop contains an if, else pair is because of the naming convention you have used. The first one doesn't contain a number next to num:

    pic1.src=bild[num].src;

    If that contained zero, then this code could be altered, and made shorter, so as to exclude the if, else pair.

    If I knew exactly what it is you were doing I may be able to help you better, as it stands this forms what you wanted into a series of strings stored in an array. You can see this if you run the code below (after the code above of course):

    var j;
    for (j=1; j < newarray.length; j++)
    {
    document.write(newarray[j] + "<br>")
    }
    Last edited by MikeOS; 11-23-2002 at 07:43 PM.

  3. #3
    Join Date
    Nov 2002
    Posts
    3

    i want to do this ...

    a picture changing script:

    <script language="JavaScript">
    <!--
    bild = new Array();
    anzahl=7;
    for (var i=1;i<=anzahl;i++) { bild[i] = new Image(); bild[i].src = "fotos/1x1.gif"; }
    for (i=anzahl+1;i<=2*anzahl;i++) { bild[i] = new Image(); bild[i].src = "fotos/toscana/0"+(i-anzahl)+".gif"; }
    num=1;
    maxpix=14;

    function changer(status)
    {
    if(status=="showhide") { num=num+anzahl; }

    if(num<=0){ num=1; }
    if(num>maxpix){ num=maxpix; }

    for (i=1;i<=anzahl;i++) { pic1.src=bild[num+(i-1)].src; }
    }


    so all the for-part belongs to the script ...
    so, can i do better the zero for i=0 ... shouldn't be a problem
    i could just add "+1" in the num-part.

    was that any help ?

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    The following might work:

    for(i=1,n=num;i<=7;i++,n++) {eval('pic'+i+'=build[n].src')}

  5. #5
    Join Date
    Nov 2002
    Posts
    3

    yippie, thx a lot

    the eval()-thing ;-)

    seems to be what i was looking for ...
    thank you

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    I thought it might be. You are quite welcome.

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