Skip to content

Instantly share code, notes, and snippets.

@jamiejohnsonkc
Last active May 28, 2021 20:39
Show Gist options
  • Save jamiejohnsonkc/a40df074721c36e9a4ed2ccdfca2fb71 to your computer and use it in GitHub Desktop.
Save jamiejohnsonkc/a40df074721c36e9a4ed2ccdfca2fb71 to your computer and use it in GitHub Desktop.
js loop output prime numbers
// For each i in the interval {
// check if i has a divisor from 1..i
// if yes => the value is not a prime
// if no => the value is a prime, show it
// }
let n = 10;
nextPrime:
for (let i = 2; i <= n; i++) { // for each i...
for (let j = 2; j < i; j++) { // look for a divisor..
if (i % j == 0) continue nextPrime; // not a prime, go next i
}
alert( i ); // a prime
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment