Click to See Complete Forum and Search --> : Can someone tell me what this means ?


2 peachy
05-19-2003, 06:35 PM
function Check( )
{
var str = “hello world”;
var x = 1;

switch (str.charAt(x))
{
case ‘w’:
x++;
break;
case ‘e’:
x--;
case ‘d’:
x += 4;
break;
default:
x = 0;
}

return (++x);
}

Jona
05-19-2003, 07:02 PM
Same as this:

function check(){
var myString = "hello world";
var myNumber = 1;

if(myString.charAt(myNumber) == "w"){
x++; // x now equals 2
} else if(myString.charAt(myNumber) == "e"){
x--; // x will equal one less than its current value
} else if(myString.charAt(myNumer) == "d"){
x=x+4; //x will now equal four plus its own value
} else { //default
x=0;
}

return (++x); // return x's incremented value
}

The switch() statement is a shortened method of the if()/else satements. It's a shorter, yet less logical method of doing the same thing. myString.charAt(myNumber) gets the letter in the string of the equivalent number. Example: "hello".charAt(1) will equal "h."

2 peachy
05-19-2003, 07:06 PM
so if x = 0, then x=x+4 would equal 4 ?

and if this was a function call in a onSubmit event handler,
would it get submited ?

Jona
05-19-2003, 07:25 PM
My bad. I made a mistake on the code. Let me show you what it's doing from the original perspective:


function Check(){
var str = "hello world";
var x = 1;

switch(str.charAt(x)){ // str.charAt(x) is equal to "e" because 0 is h, and 1 is e.
case "w": x++; alert(x); break; // if it is "w" make x+=1, so if x = 1, x now equals 2
case "e": x--; alert(x); break; // if it is "e" and it is, make x one less than it is, or decrement.
case "d": x+=4; alert(x); break; // if it is "d" make x+=4, or x=x+4
default: x=0; // if it's none of the above, x will equal 0
}
alert(++x);
return (++x);
}


If you run this function, the return value will be one (1). This is because when you run it, str.charAt(x) is equal to "e." Which means x will then equal zero (0). Then it goes through the whole thing again because that's what the switch() statement does... Kinda like an if/else loop in a for() loop... Only a lot shorter. Anyways, it runs again and finds that str.charAt(x) is now "h." So since that's not on the list, it quits. The last alert alerts, "1." Because ++x is, obviously, incrementing it.

Jona
05-19-2003, 07:27 PM
Originally posted by 2 peachy
so if x = 0, then x=x+4 would equal 4 ?
No:

if(x==0){ x+=4; } // makes x now equal 4
Originally posted by 2 peachy
..and if this was a function call in a onSubmit event handler,
would it get submited?

Yes, because the return value is a number--and since *any* number returned *will* exist, the return value is always true.

2 peachy
05-19-2003, 07:29 PM
lol... you lost me...
0 = h
1= e
2= l
3=l ???
4 = o ??


like that ?

Jona
05-19-2003, 07:33 PM
Exactly. You have dealth with arrays, true? Example:


var myAry = new Array("one", "two", "three", "four", "five");
for(i=0; i<myAry.length; i++){ //variable i equals zero, i will be less than 5, now increment (go through everything in brackets until you reach five)
alert(myAry[i]); // returns 5 alerts with their respective values.
}
alert(myAry[0]); //returns, "one"
alert(myAry[1]); // returns, "two"
//etc...


Note that I used a for() loop to demonstrate a faster way, and that if you do not put your for() loop in brackets it will loop through the entire function over and over until the variable (i in this case) reaches its specified limit, in this case 5.

2 peachy
05-19-2003, 07:40 PM
smiles... thankyou ... I think I am starting to understand more.
I worked with arrays in Flash when I was trying to get the date on a flash movie.

Jona
05-19-2003, 07:44 PM
It's no problem. I enjoy helping... Otherwise I wouldn't be here. lol

2 peachy
05-19-2003, 07:49 PM
all of you in here that take your time to help others, are truely amazing people.
I greatly appreciate all the help all of you have given me..and hopefully someday, I will be in the position to do the same.

Jona
05-19-2003, 07:52 PM
Yes, very hopefully. Thank you for the complements.