An if condition inside a loop

Viewed 30

I have a flag at the global level. There is a loop which has two parts of logic, the first one should evaluate every time, the second part should evaluate only if the global flag is set.

var flag;

for (var i = 0; i < len; i++) {
 // statements #1
 if (flag) {
  // statements #2
 }
}

I'm wondering whether there is a way to write this so that the if check doesn't happen every time. If this was Java, I'd write these as separate functions and use Function f; if(flag) f = firstFunction.andThen(secondFunction)and then iterate the array. How do I compose functions on my own in Javascript?

How would I solve this?

0 Answers
Related