PDA

View Full Version : Identifying a line of script



Softie2k
04-25-2008, 03:55 AM
var pick1 = parseInt(document.myForm.******.selectedIndex)==null?1:parseInt(document.myForm.*****.selectedIndex);

Could someone please help me by explaining how this line in a script works?

Basically I have 2 lists that will be added together like dates.

January in one list and 3rd in another form = January 3rd.

Thanks!

Nile
04-25-2008, 04:01 AM
I think that this is an if statement, a short one:


var * = ((condition) ? if its true : otherwise);

I suggest making that line of code into this:


var pick1 = ((parseInt(document.myForm.******.selectedIndex)==null)? 1 : parseInt(document.myForm.*****.selectedIndex));

Well, thats what I've been told.
Hope this Helps,
Nile

Softie2k
04-25-2008, 04:08 AM
thanks very much!

rangana
04-25-2008, 04:08 AM
var pick1 = parseInt(document.myForm.******.selectedIndex)==null?1:parseInt(document.myForm.*****.selectedIndex);
Thanks!

This is a ternary operation..a shorthand of the if statement :)

Which means exactly the same as:


if (parseInt(document.myForm.******.selectedIndex)==null)
{
var pick 1 = 1;
}
else{
pick1 = parseInt(document.myForm.******.selectedIndex);}

Nile
04-25-2008, 04:15 AM
He is very well right, except for the fact that he has a space in the first if(){ inbetween his pick and 1.


This is a ternary operation..a shorthand of the if statement :)

Which means exactly the same as:


if (parseInt(document.myForm.******.selectedIndex)==null)
{
var pick 1 = 1;
}
else{
pick1 = parseInt(document.myForm.******.selectedIndex);}

rangana
04-25-2008, 04:19 AM
Yes Nile, you're right...you hit me on my head ;)

Nile
04-25-2008, 04:39 AM
Ah, anytime. ;)

codeexploiter
04-25-2008, 05:09 AM
Any of the following will make the operation work.


var pick1 = document.myForm.******.selectedIndex == null ? 1 : document.myForm.*****.selectedIndex;

var pick1 = (document.myForm.******.selectedIndex == null) ? 1 : document.myForm.*****.selectedIndex;

It is not necessary to use parseInt as the selectedIndex used to return an index of the type number.

Nile
04-25-2008, 05:13 AM
Codeexploiter, you can also put the whole thing in a ( ), like this:


var pick1 = ((document.myForm.******.selectedIndex == null) ? 1 : document.myForm.*****.selectedIndex);

codeexploiter
04-25-2008, 05:20 AM
Yes of course but personally I feel that it is necessary especially if we are constructing a really complex operation using ternary operator. The one discussing here is a single operation equivalent to a simple if else condition.

Nile
04-25-2008, 05:24 AM
Yeah, well I gave it all I got, we've explained it, and its solved. So I'll bring that to mind next time I'm making an if statement.

codeexploiter
04-25-2008, 05:27 AM
Yeah, well I gave it all I got, we've explained it, and its solved. So I'll bring that to mind next time I'm making an if statement.

For the if statement you have to use parentheses in JS and in most of the languages (a few exceptions are there too) :D