We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Continue

The continue keyword stops the current iteration of a loop and immediately moves on to the next one.

for (let i = 0; i < 10; i++) {
  if (i % 2 === 0) {
    continue;
  }
  console.log(i);
}
// Prints:
// 1
// 3
// 5
// 7
// 9

Assignment

...I'm gonna break the 4th wall and not even pretend this assignment has anything to do with "Textio". The prime-checking logic is mapped out below. Focus on using continue and break in the right spots.

Complete the printPrimes function. It should print all of the prime numbers up to and including max. It should skip any numbers that are not prime.

Use this checklist:

for n from 2 to max (inclusive):
  if n is 2:
    n is prime, print it
  if n is even:
    n is not prime, skip to next n
  for i from 3 to the square root of n (inclusive) in steps of 2:
    if n can be evenly divided by i:
      n is not prime, stop checking factors
  n is prime, print it
  • We skip even numbers because they can't be prime
  • We only check up to the square root of n. A factor higher than the square root of n must multiply with a factor lower than the square root of n, meaning it has no chance of multiplying evenly into n.
    • In your code, you can set the stop condition as i * i <= n
  • We start checking at 2 because 1 is not prime, so don't print it!

This lesson is graded based on the output of your program, so don't leave any debugging print statements in your code.

Example Output

The primes up to 10 are:

2
3
5
7

Only print the numbers themselves, no headings or delimiters.

Tip

Use continue when n is 2, and again when n is even. For the inner loop, use break to stop checking factors early. Then only print the number after the inner loop if you never found a factor.