Not (!) operator , how does it really work, secondly how does this method account for prime 5 if n is set to limit of 10? | SoloLearn: Learn to code for FREE!

0

Not (!) operator , how does it really work, secondly how does this method account for prime 5 if n is set to limit of 10?

this code is used to find prime numbers function showPrime(n){ for( let x=2; x<n; x++){ if( !isPrime(x) ) continue; document.write(x); } } function isPrime(n){ for( let x=2; x<n; x++){ if(n%x==0) return false; } return true; } //n%x let's say n is 10 and x is 5. remainder is 0 ....hence false is returned but 5 is prime ,not sure on this. lastly the return values are being stored in !isPrime(x)...?

12/15/2017 6:12:47 AM

Louis McIntosh

1 Answer

New Answer

+4

When you write n%x you test whether n is divisible by x, like 10%5 == 0 --> You test here if 10 is a prime, not 5. If it returns false because of 10%5 == 0, it means 10 isn't a prime, n has to be the tested number. Hope you understand what I want to say😊