Why does the 1st code not work but the 2nd does

1st

while(i >= 2) {
const res = isPrime(i)
if(!res) {
  continue;
} else {
  para.textContent += i +','
}
i--
}

// Output: 500,

2nd

while(i >= 2) {
const res = isPrime(i)
if(res) {
  para.textContent += i +','
} 
i--
}

// Output: prime number

I believe the use of continue is going to skip all the numbers that don’t meet the criteral but that is not the case. Why?

Hello, Gbemiga,
I’m not affiliated with Mozilla; I’m a fellow learner, like you. I played around with a similar loop construct on W3Schools.com, and I can at least largely answer your question.

Assuming you start the loop with i = 500, the first iteration checks to see if 500 is prime. Since it isn’t, the continue statement gets run. Execution then skips the rest of the loop, including the decrement, and returns to the while statement. The problem is, i is still 500 at this point. The same things happen again, and you end up with an infinite loop because i is never decremented. I don’t see, then, how you got your output. The else clause would have to be run to produce it, and I don’t see how it ever could be.

Try moving the decrement up before the const res = isPrime(i) statement. A couple of other changes would have to be made to preserve the intended behavior. I’ll leave it for you to figure out what they are.

And by the way, you’re missing the ending semicolon on all your statements except the continue. That didn’t seem to make any difference, however, when I tried it at W3Schools.

Hi, Wjruther. I thought that JavaScript runs code line by line. Which means regardless of which code runs in the if/else block, the decrement should still occur.

What really gets me scratching my head is figuring out if I should first check for false or truth when using if/else statements in general. That’s a major drawback.

I.e

If(!test) {
    // Do something 
  }

Or

If(test) {
    // Do something 
  }

Hi, Gbemiga. It’s generally true that JavaScript runs line by line. And your first code would run the way you’re expecting it to if the continue were a simple do-nothing statement, like it is in Fortran, for example. But in JavaScript, it isn’t; it alters the normal flow of execution. Once encountered, it immediately terminates the current iteration of the loop and begins the next one. Everything in the loop that comes after it is skipped. So it’s important that the loop control variable be changed before the continue is executed, or the program will be trapped in an infinite loop.

As for the false/truth tests, both work equally well as far as the computer is concerned. From a pure code execution standpoint, it doesn’t matter which one you choose. The choice should be made based on which one yields simpler, more understandable, more readable code. In most cases, probably, it will be the truth test. But it depends on the particular circumstances. In your example code, the truth test is clearly superior. It makes the code a little shorter and more understandable. In this case, the false test requires a little more code, and led to the introduction of a bug when you apparently misunderstood the usage of the continue statement.

This led me to do a search on JavaScript noop. It turns out that JavaScript doesn’t have a no-operation statement like Fortran’s continue or Python’s pass. I did find several ways to implement it as a function. I haven’t done enough research at this point to figure out which way is best.