function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
alert(i);
}
}
function isPrime(n) {
for (let i = 2; i < n; i++) {
if ( n % i == 0) return false;
}
return true;
}
showPrimes(20)
Here’s a code I have been trying to study/understand. So my understanding of how the code works has been:
Let’s assume that n = 20
First it will execute the showPrime(20) function body, so we will start with “2”, since we have the postfix operator which will return the old value of “i = 2”.
Then we will execute function isPrime(20), since 2 < 20 = true, we also check (20 % 2 == 0) which is true.
The if function in showPrimes(20) show “!isPrime(i)”, and since the default boolean value is “false”, we check for whether isPrime is NOT false which = true.
So we know that (20 % 2 == 0) is true, so it’ll continue executing showPrimes(20) which does alert(i); , in this case being “2”.
Now we move on to i = 3 and carry out isPrime(20) again.
The test for 3 < 20 is true, so then we test (20 % 3 == 0) which is false.
And here is where I’m stuck. Since (20 % 3 == 0) returns true, so we just continue on with the inner loop? I am confused since 3 is also a prime number so it should alert us of that number as well.
If someone can please help me figure out how these types of functions work, I’d greatly appreciate it. I really don’t even know if my thought process is correct. For a second I did think I figured it out but keep getting lost.
Alright, already suspected there was missing a bit more than a curly brace…
Little mistake here, the function call is isPrime(i), not isPrime(n), so at this point in the loop, isPrime(2) is called, checks if 2 is prime - which is true, so the if-block is skipped and you’ll get the alert with i=2.
The loop in showPrime will keep running and in the next step, it’ll call isPrime(3). That returns true again, so !isPrime(3) is false, the if-block is skipped and you get the alert with i=3.
In the next step, isPrime(4) will return false → !isPrime(4) will be true, so the loop just continues without alert.
And so on…
So, instead of calling isPrime(20) again and again, the function argument changes each time. It’s also important to realise that the i in the showPrimes for loop is NOT the same as in the isPrime for loop. Those are completely independent from each other. Both i variables are restricted to the scope of the function that they’re declared in. One function cannot see the i value of the other.
I’m still not quite understanding how the prime numbers are calculated.
So say we’re testing for i = 2 for showPrimes(20) and isPrime(20).
Then (20 % 2 == 0) would return false right?
since
if ( n % i == 0) return false;
Then wouldn’t we do (20 % 2 ==0)? Which returns false, making the isPrime loop continue?
Also, what do you mean by the function arguments changing each time?
If we call function showPrime(20), then how would isPrime(n) know what the value of N is?
I mean this part - you call isPrime with whaterver the value of i is in that moment.
You know what.
Change the variable names so you don’t have the same name used for more than one variable, even if they are in different scopes.
Something like this:
function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
alert(i);
}
}
function isPrime(a) {
for (let b = 2; b < a; b++) {
if ( a % b == 0) return false;
}
return true;
}
showPrimes(20)
isPrime returns true, so then the execution returns to the other function, alert(2) is executed, and the loop goes to i=3, so now isPrime(3) is executed, which also returns true, and alert(3) happens
I would have written like this inside the loop: if (isPrime(i)) console.log(i)
or if (isPrime(i)) alert(i)
as it’s much clearer what’s going on
How would isPrime(2) return true?
If the “b < a” condition is not met, it should be false no?
and then (2 % 2 ==0) would also return false shouldn’t it?
I see. Then what about for a number like 15?
(15 % 2 == 0) would return false, so would it test for (15 % 3 == 0)?
That would then return true wouldn’t it?