0

I am working on some code in Node.js. Basically I am trying to analyze and apply logic on the query part of the URL. The abstract of my situation is below:

If(condition1){run1};
If(condition2){run2};
else{run3};

When F-F (condition1-condition2) run3 happens When T-F run3 happens (run1 should've happened...) When F-T run2 happens When T-T run1 and run2 happen

I tried:

If(condition1){run1};
else{run4};
If(condition2){run2};
else{run3};

Where run4 is blank.

What is going on here? I want run1 to happen if condition1 is met, run to happen if condition2 is met and both if both are met and none if none are met.

TIA

1
  • @jahroy I have just updated my answer after going through the question again. Commented Aug 7, 2013 at 1:04

2 Answers 2

4
if (condition1) {
    run1;
}
if (condition2) {
    run2;
}
if (! condition1 && ! condition2) {
    run3;
}
Sign up to request clarification or add additional context in comments.

4 Comments

so the presence of an else{} in the same bracket alters the behavior of preceding if{}'s within the same bracket?
No... Your logic is just incorrect. Try stepping through your code. You'll see why you're not getting what you want. In the example you provided, if condition1 is true and condition2 is false, run1 and run3 will both execute.
No to what exactly? and a run1 and run3 in that scenario is what I want but does not happen. With your format it does work. Im interested to know why it works.
@nemo - No: the presence of an else in the same bracket does not alter the behavior of preceeding ifs. Your code did not work because your if/else statements were structured wrong. If you step through your code when condition1 is true and condition2 is false, you'll see why your original code was incorrect. It will execute run1 and run3 which is not the behavior you described.
-2

Below is the correct solution with proper brackets

If(condition1){run1};
If(condition2){run2};
If(!condition2 && !condition2){run3};

1 Comment

that is not helpful. please re-read my question. if condition1 and condition2 are met only run1 will execute in your solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.