Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Conditional Operator for else if?

Engaged ,
Oct 16, 2012 Oct 16, 2012

I used the google compiler on one of my scripts. It worked fine except I am getting hung up on some of the conditional statements it made. Originally I had a series of if else if statements. When changed to Conditional Operator it changes the fuctionality of it. Here we go if anyone can help I will take any tips.

My version:

if(myYear.selection.text=="10"){yearSelect=0;}

         else if(myYear.selection.text=="11"){yearSelect=1;}

         else if(myYear.selection.text=="12"){yearSelect=2;}

         else if(myYear.selection.text=="13"){yearSelect=3;}

         else if(myYear.selection.text=="14"){yearSelect=4;}

        else{yearSelect=5;}

Google version:

yearSelect="10"==b.selection.text?0:"11"==b.selection.text?1:"12"==b.selection.text?2:"13"==b.selection.text?3:"14"==b.selection.text?4:5,

Thanks in advance for any advice.

Brett.

TOPICS
Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 16, 2012 Oct 16, 2012

Common Javascript syntax.

a = b ? c : d;

is equal to

if (b == true)

  a = c;

else

  a = d;

What does "it changes the fuctionality of it" mean? Doesn't it work anymore?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 16, 2012 Oct 16, 2012

.. Apart from that, there is no need for your original long list of "if .. else". For a list of possible inputs, you should consider something like this:

switch(myYear.selection.text)

{

case "10": yearSelect=0; break;

case "11": yearSelect=1; break;

case "12": yearSelect=2; break;

case "13": yearSelect=3; break;

case "14": yearSelect=4; break;

default: yearSelect=5;

}

or, with your input and output, something as simple as

yearSelect = Number(myYear.selection.text)-10;

-- but I don't know what your possible range of inputs is. If it's "anything goes", you have to check for a not-in-range and then set it to your default of 5.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 16, 2012 Oct 16, 2012

I just tried this and it worked thanks you for your help!

yearSelect = yearSelect="10"==b.selection.text?0:

          yearSelect="11"==b.selection.text?1:

          yearSelect="12"==b.selection.text?2:

          yearSelect="13"==b.selection.text?3:

          yearSelect="14"==b.selection.text?4:

                       5,

I will also consider the switch. Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 16, 2012 Oct 16, 2012

Brett,

The difference between Jongware's version and your last one is that Jongware's version is easy to understand, while your last version is not. Why would you entertain such a braintwister when there's such a clear method available? that Google comes up with those constructions doesn't mean much.

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 30, 2012 Nov 30, 2012
LATEST

Thanks Peter. Very good to have your perspective.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines