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

Closing an If statement without using "Else"

Participant ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Hi everyone,

 

I´d like to know if it is possible to close an "if statement" without using "else if" or "else", in case there is only one condition to be met.

My code is as follows:

 

If (Text15 == "BOILING") {

app.alert ("Correct answer. Congratulations!")

}

else if (Text15 !== "BOILING") {

app.alert (Incorrect answer. Please check for any misspelling.");

 

*** end of code***

 

Is it aceptable to finish the code with only one "else if" and not using an "else" statement after it, since I don´t have any other conditions to check?

If not, how can I close these sequence of conditions, since I won´t need an "else" statement?

 

Thanks a lot for your clarification in advance.

TOPICS
Create PDFs , JavaScript

Views

843

Translate

Translate

Report

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

correct answers 1 Correct answer

LEGEND , Mar 17, 2021 Mar 17, 2021

This would typically be written as:

 

if (Text15 == "BOILING") {
    app.alert ("Correct answer. Congratulations!");
} else {
    app.alert ("Incorrect answer. Please check for any misspelling.");
}

Votes

Translate

Translate
Community Expert ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Well, you're actually declaring and evaluating two conditions in your script, not just one.

 

If you're using this event as custom calculation script, just close the "else if" condition with the curly brace "}".

 

There's no need to add anything else.

 

 

 

Votes

Translate

Translate

Report

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
Participant ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Thanks for your prompt reply and clarification.

 

I´ve actually mentioned having only "one condition" hypothetically. 

 

So, my code seems correct, but I keep getting error messages from the debugger saying there is

a sintax error on line 6 (the line just after the "else if" statement).

 

I even re-wrote the code after creating a new text field and a button, but the error message keeps popping up.

 

Don´t know where else to turn to.

Votes

Translate

Translate

Report

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
Advisor ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Hello Andres,

As mentioned, your code is missing the closing curly brace "}" after the "else if" statement.

 

If (Text15 == "BOILING") {

app.alert ("Correct answer. Congratulations!")

}

else if (Text15 !== "BOILING") {

app.alert (Incorrect answer. Please check for any misspelling.");

}


 
Regards,
Mike

Votes

Translate

Translate

Report

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
Participant ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Thanks a lot. The code is working properly now.

Votes

Translate

Translate

Report

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
LEGEND ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

This would typically be written as:

 

if (Text15 == "BOILING") {
    app.alert ("Correct answer. Congratulations!");
} else {
    app.alert ("Incorrect answer. Please check for any misspelling.");
}

Votes

Translate

Translate

Report

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
Participant ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

Thanks a lot for your reply.

 

Now I am aware that a semi-colon is needed after the closing of each instruction (app.alert).

The code is working properly now.

 

 

Votes

Translate

Translate

Report

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 ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

LATEST

It's not really needed, but it's good practice to add it.

Also, adding "else" or "else if" is not needed, either.

Votes

Translate

Translate

Report

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