Question about a Javascript function made to check for prime numbers

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

  1. 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”.

  2. Then we will execute function isPrime(20), since 2 < 20 = true, we also check (20 % 2 == 0) which is true.

  3. 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.

  4. 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”.

  5. Now we move on to i = 3 and carry out isPrime(20) again.

  6. The test for 3 < 20 is true, so then we test (20 % 3 == 0) which is false.

  7. 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.

Thanks ,
Peter

You’re missing the closing curly brace:

function showPrimes(n) {

}

what’s your question? what’s the function you are asking about?

Sorry I accidentally pressed reply while typing out my question. I just edited it so feel free to take a look.

Thank you

Hi,

I was typing out my question and clicked submit on accident. Just edited the code and my questions in. Please feel free to take a look.

Much appreciated,
Peter

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.

1 Like

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?

Thanks for answering,
Peter

is this about the i++? in a loop it doesn’t matter, because the incrementing part is executed on its own - you get the exact same behaviour using ++i

no, the first executed is isPrime(2) - the first value that i takes is 2

you can see the execution with this if you want:

http://pythontutor.com/javascript.html

I just checked out the website you linked…
What does it mean by i (block 1) and i (block 2)??

weird, just ignore it, it will be fine, consider only the variable i that changes

Then how is n = 2 for isPrime? Even though we never declared isPrime(2).

you call isPrime(i) inside a loop for (let i = 2; i <= n; i++) (where n=20), so i takes as value all the numbers from 2 to 20

Yes , but the variable in isPrime is “n”, not “i” .
I am so confused haha

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)

Okay. so since i=2 it will make a=2 as well, just for the first iteration. But then 2 < 2 is not true, so what happens then?

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?

For isPrime(2), the loop doesn’t even run once, so it skips the whole loop and only runs the return true; line.

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?

This means that if a % b == 0, then the function returns false. If a % b == 0 is never true, for any 2 <= b < a, then the function return true.