0

I'm trying to stretch my Javascript knowledge so I'm setting little targets for myself. I wanted to log the prime numbers between 1-100, and I created this code. How can this be improved in vanilla Javascript?

for (var i=2; i<=100; i++) {
    // specifically identify 2,3,5,7
    else if (counter === 2 || counter === 3 || counter === 5 || counter === 7 ) { 
        console.log(counter); 
    }
    // anything that doesn't divide by 2,3,5,7 is prime
    else if (counter % 2 !== 0 && counter % 3 !== 0 && counter % 5 !== 0 && counter % 7 !== 0) { 
        console.log(counter);
    }
}

I'm aware this might be closed, but I wanted to make sure there's not a glaring error or easier solution staring me in the face. Thanks!

authorandrew
  • 145
  • 2
  • 10
  • Search function needs serious improvement if this is a duplicate, which it clearly is. I did three distinct searches for various combinations of 'Javascript prime numbers 1-100' and nothing came up that was relevant. – authorandrew Jan 19 '15 at 00:41
  • 1
    I would use `counter%1 && counter%2 && counter%3 ...`. You don't have to compare to `0`, because only the number `0` (and `NaN`) is falsy. – Oriol Jan 19 '15 at 00:45
  • 1
    Yes, I think they are already working to improve the search engine using fuzzy logic. – Oriol Jan 19 '15 at 00:49

0 Answers0