Jump to content

Sometimes the simplest things


george

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?

 

Link to comment
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 }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...