Why doesn't this for loop work?

M

[Mr.] Lynn Kurtz

I have a sequence of four document.write commands that work. The
swap() function swaps in a new picture and the carstages array is a
list of what the picture represents. So you under the picture you geth
a line of text links links on which to click to see the next picture.
And it all works (Yay!!).

But here's my question-- when I comment out the four individual
instructions and replace them with the loop, I get a picture, but I
don't get the line of links underneath anymore to click on. Probably
something stupid, but I never claimed to be an expert. :-(

Any suggestions? Here's the code (this is in the body section):

<script>type="text/javascript">

// problem with this loop:
// for(j = 0; j < carstages.length; j++)
// document.write('<a ' + 'href="javascript:swap(j)">'
// + '<em>'+ carstages[j] + '<\/em>'+ '<\/a>'+'&nbsp');

document.write('<a ' + 'href="javascript:swap(0)">'
+ '<em>'+ carstages[0] + '<\/em>'+ '<\/a>'+'&nbsp');

document.write('<a ' + 'href="javascript:swap(1)">'
+ '<em>'+ carstages[1] + '<\/em>' + '<\/a>'+'&nbsp');

document.write('<a ' + 'href="javascript:swap(2)">'
+ '<em>'+ carstages[2] + '<\/em>' + '<\/a>'+'&nbsp');

document.write('<a ' + 'href="javascript:swap(3)">'
+ '<em>'+ carstages[3] + '<\/em>' + '<\/a>'+'&nbsp');

document.write('<a ' + 'href="javascript:swap(4)">'
+ '<em>'+ carstages[4] + '<\/em>' + '<\/a>'+'&nbsp');
</script>

The swap function is in the header:

function swap(imgIndex)
}
document['imgMain'].src = carimages[imgIndex];
}

and the carstages array is just an array of strings.

Thanks.

--Lynn
 
R

RobG

I have a sequence of four document.write commands that work. The
swap() function swaps in a new picture and the carstages array is a
list of what the picture represents. So you under the picture you geth
a line of text links links on which to click to see the next picture.
And it all works (Yay!!).

But here's my question-- when I comment out the four individual
instructions and replace them with the loop, I get a picture, but I
don't get the line of links underneath anymore to click on. Probably
something stupid, but I never claimed to be an expert. :-(

Any suggestions? Here's the code (this is in the body section):

<script>type="text/javascript">

// problem with this loop:
// for(j = 0; j < carstages.length; j++)

The body of a for loop is supposed to be enclosed in {} (curley
braces). You can omit the braces if the body is only one statement,
but for more than one statement you must use braces:

for(j = 0; j < carstages.length; j++) {
/* stuff here */
}

Your loop body is only one statement, however it is long so I'd
suggest using braces. It makes maintenance that much easier.

// document.write('<a ' + 'href="javascript:swap(j)">'
// + '<em>'+ carstages[j] + '<\/em>'+ '<\/a>'+'&nbsp');

You have not use "var" to make the scope of j local, so it is global.
When the function runs the first time, it loops until j is set to
carstages.length, whereupon it ends. When you later call swap(j), all
the functions reference this last value of j. Since
carstages[carstages.length] never exists (the length is always one
greater than the highest index), you get an error.

I suggest that you don't use javascript within the href attribute, and
instead set it to something useful. Then use an onclick handler to
grab the (now useful) href attribute to swap the image and, if
successful, return false. Or put the index into the ID of the a
element and use that, e.g..

document.write('<a ' + 'href="' + carstages[j] + '" id="img_' + j +
'"
+ 'onclick="swap(this.id);return false;">'
+ '<em>'+ carstages[0] + '<\/em>'+ '<\/a>'+'&nbsp');

Untested, but you should get the idea.

[...]
The swap function is in the header:

function swap(imgIndex)
}

Given that you are now passing an ID like "img_0" you can use
something like:

var imgIndex = imgIndex.split('_')[1];

document['imgMain'].src = carimages[imgIndex];
}

and the carstages array is just an array of strings.
 
G

Gregor Kofler

[Mr.] Lynn Kurtz meinte:
I have a sequence of four document.write commands that work. The
swap() function swaps in a new picture and the carstages array is a
list of what the picture represents. So you under the picture you geth
a line of text links links on which to click to see the next picture.
And it all works (Yay!!).

But here's my question-- when I comment out the four individual
instructions and replace them with the loop, I get a picture, but I
don't get the line of links underneath anymore to click on. Probably
something stupid, but I never claimed to be an expert. :-(

Any suggestions? Here's the code (this is in the body section):
<script>type="text/javascript">

// problem with this loop:
// for(j = 0; j < carstages.length; j++)
// document.write('<a ' + 'href="javascript:swap(j)">'

...."javascript:swap('+j.toString()+')"...

There are various other weaknesses in your script (click handler instead
of pseudo-protocol, scopes of variables), but this seems to be the core
error.

Gregor
 
G

Gregor Kofler

Gregor Kofler meinte:
..."javascript:swap('+j.toString()+')"...

There are various other weaknesses in your script (click handler instead
of pseudo-protocol, scopes of variables), but this seems to be the core
error.

Together with the alredy mentioned curly braces.

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
<script text...>


..."javascript:swap('+j.toString()+')"...

Calling .toString() is unnecessary as the concatenation operation will call
that implicitly already.

..."javascript:swap('+j+')"...
There are various other weaknesses in your script (click handler instead
of pseudo-protocol,

You mean the weakness of using a pseudo-protocol instead of a click handler,
not vice-versa.
scopes of variables), but this seems to be the core error.

ACK


Regards,

Pointed"Goodnight? ;-)"Ears
 
M

[Mr.] Lynn Kurtz

<script type="text/javascript">

Ahh, yes, thanks.
Calling .toString() is unnecessary as the concatenation operation will call
that implicitly already.

..."javascript:swap('+j+')"...

And thanks again..
You mean the weakness of using a pseudo-protocol instead of a click handler,
not vice-versa.

Thanks to all who replied. I have the loop working now. Next step is
to read about click handlers I guess. Great group here.

Cheers.

--Lynn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top