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!
New course! Every coder should learn Generative AI!
Try a free lesson
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)...?

15th Dec 2017, 6:12 AM
Louis McIntosh
Louis McIntosh - avatar
1 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😊
15th Dec 2017, 6:26 AM
Jonas Schröter
Jonas Schröter - avatar