Jump to content
george

Sometimes the simplest things

Recommended Posts

Within script tags in the body of my HTML, I have:

aRay = new Array();
aRay[0] = 'a';
aRay[1] = 'b';
aRay[2] = 'c';
aRay[3] = 'd';
aRay[4] = 'e';
document.write("aRay length is "+aRay.length+"</br>");
for( i = 0; i < aRay.length; i++ )
 document.write("element "+i+" is "+aRay[i]+"</br>");

for( y = 0; y < aRay.length; y++ ) {
  if(aRay[y]='d')
    document.write("so we can find ""+aRay[y]+"" in element "+y+"</br>");
	break;
  endif;
}

And what prints out is:

aRay length is 5
element 0 is a
element 1 is b
element 2 is c
element 3 is d
element 4 is e
so we can find "d" in element 0

That 0 at the end of the line above should be a 3.

Why isn't it?

What am I missing here?

 

Share this post


Link to post
Share on other sites

This is an assignment. You're setting all the elements in the array to "d"

aRay[y]='d'

Also, in Javascript there is no endif statement. Code within the if() block should be wrapped in { curly braces }

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...