Anon Symphony
Anon Symphony

Reputation: 47

Continue on the next else if if value is not true

I have written a code that checks some conditions.

But i got stumble on this part. I have used the continue; but it doesn't run and says it has an error.

if (support_Fixednodes !== undefined && support_Fixednodes !== null){

    if ((arrayIndex.length - 1) === arrayIndex.lastIndexOf("FIXED")){

      var supportFBnode_index = checkIfElementExist(dataTwo, "FIXED");

    }else if ((dataTwo.length - 1) !== dataTwo.lastIndexOf("FIXED")){

      continue; <-- This doesn't work.if this part of IF is true i want it to 
                    jump to the next else if

    }


}else if (check_PinnedSupports.BoleanValue === "Existing"){   <----jump here

  var supportFBnode_index = checkIfElementExist(dataTwo, "PINNED");

}

Upvotes: 0

Views: 519

Answers (2)

Jaromanda X
Jaromanda X

Reputation: 1

to avoid repeated conditionals and repeated code is possible using a little known javascript construct ... the label

depending on what jump here is actually referring to (before the if, or the body of the if)

So, before the if (so the last if is checked regardless)

var supportFBnode_index;
hack: {
    if (support_Fixednodes !== undefined && support_Fixednodes !== null) {
        if ((arrayIndex.length - 1) === arrayIndex.lastIndexOf("FIXED")){
            supportFBnode_index = checkIfElementExist(dataTwo, "FIXED");
            break hack; // DO NOT RUN THE LAST IF
        } else if ((dataTwo.length - 1) === dataTwo.lastIndexOf("FIXED")) { // changed to === so, if !==, next if gets run
            break hack; // DO NOT RUN THE LAST IF
        }
    }
    // the jump in your code refers to this point
    if (check_PinnedSupports.BoleanValue === "Existing") {
        supportFBnode_index = checkIfElementExist(dataTwo, "PINNED");
    }
}

or, body of the if, i.e. the code inside the last if is the destination for the jump

var supportFBnode_index;
hack: {
    if (support_Fixednodes !== undefined && support_Fixednodes !== null) {
        if ((arrayIndex.length - 1) === arrayIndex.lastIndexOf("FIXED")) {
            supportFBnode_index = checkIfElementExist(dataTwo, "FIXED");
            break hack; // DO NOT RUN THE LAST ASSIGNMENT
        } else if ((dataTwo.length - 1) === dataTwo.lastIndexOf("FIXED")) { // changed to === to skip last bit
            break hack; // DO NOT RUN THE LAST ASSIGNMENT
        }
    } else if (check_PinnedSupports.BoleanValue !== "Existing") { // changed to !== to skip last bit
        break hack; // DO NOT RUN THE LAST ASSIGNMENT
    }
    // the jump in your code refers to this point
    supportFBnode_index = checkIfElementExist(dataTwo, "PINNED");
}

So, instead of jumping to a point in the code if a condition is met, we can break past it if the condition is not met

Note the var supportFBnode_index; declaration - while having var supportFBnode_index = in two places works, it's not best practice (especially if you later change var to let) ... so that's a freebie hint :p


simple break label example

hack: {
  console.log('this will output');
  break hack;
  console.log('this wont output');
}
console.log('done');

basically, break label causes execution to skip the rest of the labelled block

As I said, a little known feature of the language, but it has its place - though, I'm sure there are other ways to avoid "repetition" in this case, but this, to me, is the most elegant

Upvotes: 0

Suhas
Suhas

Reputation: 580

break and continue statements are to be used in loops and not in if-else blocks. You should rather merge the statements using logical operators

if ((support_Fixednodes !== undefined && support_Fixednodes !== null) && ((arrayIndex.length - 1) === arrayIndex.lastIndexOf("FIXED"))){

  var supportFBnode_index = checkIfElementExist(dataTwo, "FIXED");

} else if (check_PinnedSupports.BoleanValue === "Existing" || ((support_Fixednodes !== undefined && support_Fixednodes !== null) && ((dataTwo.length - 1) !== dataTwo.lastIndexOf("FIXED")))){

  var supportFBnode_index = checkIfElementExist(dataTwo, "PINNED");

}

Using the || operator (OR) will solve your issue.

EDIT If you do not mind some redundancy in your code but want it to look cleaner, just repeat the variable's value assignment instead of continue

if (support_Fixednodes !== undefined && support_Fixednodes !== null){

    if ((arrayIndex.length - 1) === arrayIndex.lastIndexOf("FIXED")){

      var supportFBnode_index = checkIfElementExist(dataTwo, "FIXED");

    }else if ((dataTwo.length - 1) !== dataTwo.lastIndexOf("FIXED")){

      var supportFBnode_index = checkIfElementExist(dataTwo, "PINNED");

    }


}else if (check_PinnedSupports.BoleanValue === "Existing"){   <----jump here

  var supportFBnode_index = checkIfElementExist(dataTwo, "PINNED");

}

Upvotes: 2

Related Questions