0
let i = 500;
do {
    if (isPrime(i) === false) {
        continue;
    } else {
        para.textContent += i + ', ';
    }
    i--;
} while (i >= 2);

and it works well if I moved the (i--) to the start of the loop.

1
  • 2
    Because isPrime(500) is false. PS no need to test x === false, just use !x. Commented Jun 8, 2020 at 19:33

2 Answers 2

2

Your loop keeps iterating at -

if (isPrime(i) === false) {  //this condition will be true and code keeps looping because you never increment i
    continue;
}

But, in your code where you declare i-- at the beginning, i will keep decreasing no matter what. So the first one will run succesfully as expected, while in the second program, the execution will never go beyond the first if() condition.

Sign up to request clarification or add additional context in comments.

Comments

1

Initially i=500 , enter loop

if (isPrime(i) === false) {
        continue;
    }

this condition satisfies, and go to next iteration i value is 500 forever

Comments

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.