function sumPrimes(num) {
var prime = [];
//This loop references the function 'isPrime'.
//It passes the 'i' arg to 'isPrime' (which is 2 first time).
for(var i = 2; i <= num; i++) {
if(isPrime(i)) {
prime.push(i);
}
}
//Function takes 2 (i) and checks it. If it passes the for loop (j = 2; if j < num(2)), it doesn't
//pass this, so it returns false. Then j = 3??? No, J stays at 2 and i(num) becomes 3. Then it continues to fail..? COnfused because the code works. Thought I understood it.
function isPrime(num) {
for (var j = 2; j < num; j++) {
if (num % j === 0) {
return false;
}
}
return true;
}
return prime;
}
sumPrimes(20);

See my code comments. I have passed this challenge but I want some help understanding the code. I thought I understood it but canāt understand why the for loop works in the āisPrimeā function.
Any help would be much appreciated. Literally tearing my hair out over it the last few days.

@JoshuaPP isPrime( ) function checks if the number is prime or not.

The task of sumPrimes( ) function is to sum all the prime numbers up until the specified number - in your case num variable. So the for loop starts from 2 and it goes on until it reaches the num. In every iteration, it checks whether i is prime number or not using isPrime function. If it is prime, than it adds i to the primes array.

I hope that this was helpful, feel free to ask if there are still some things that are not clear

But the isPrime variable is passed the number 2 because i = 2 no? Meaning that for (var j=2; j < num(2); j++) never runs?? because j isnāt less than 2, it is equal to two(i).

I am obviously wrong, but I canāt understand why I am wrong. Sorry for the misunderstandingā¦ Itās probably really simple mistake.

function sumPrimes(numArgOne) {
var prime = [];
//This loop references the function 'isPrime'.
//It passes the 'i' arg to 'isPrime' (which is 2 first time).
for(var i = 2; i <= numArgOne; i++) {
if(isPrime(i)) {
prime.push(i);
}
}
function isPrime(numArgTwo) {
for (var j = 2; j < numArgTwo; j++) {
if (numArgTwo% j === 0) {
return false;
}
}
return true;
}
return prime;
}

I believe what confuses is you is that the same name for function parameter is used for isPrime function and sumPrimes function. The code I posted above may be easier to read and understand

Ahh, I swapped those around already. As in, I did that to help understand my confusion.

What I donāt understand is perhaps the way the looping is working and the ordering of it? So, numArgTwo = 2 now on the first run no? This means that the for loop is checked if j < numArgTwo right? And itās not, as they are both equal?

Letās try sumPrimes(5); for exampleā¦
What happends first is that variable i equals 2, and it enters the loop inside the sumPrimes function.
Then the program asks if i is a prime number - i is currently 2. The isPrime function returns a boolean true or false value. We go inside the loop of isPrime function, which will have no iteration because it starts from 2 and finishes before two, thus we have 0 iterations, and program doesnāt enter the loop, instead it goes straight to the end and returns true.

Variable i is a prime number so the array prime extends and it looks like this now

//this is prime array-------> [2]

That is the end of the first iteration of sumPrime function, i is 3 now and we start the second iteration.

Then the program asks if i is a prime number - i is currently 3. The isPrime function returns a boolean true or false value. We go inside the loop of isPrime function, which looks like this now

for( var j = 2; j < 3; j++)

Now this loop will have only one iteration, and here we see that 3%2 is not equal to 0, the iteration ends, and the loop ends, because j is 3 now.

Variable i is a prime number so the array prime extends and it looks like this now

//this is prime array-------> [2,3]

You can try to guess what happens when i reaches 4

The code made sense before, but I have since forgotten. I am trying to go back and comment things.

The problem is that I have some trouble visualising the looping sometimes and what/how it is doing things. Not sure If I am alone in having this problem

I think most people have difficulties with loops and arrays first time they meet them. But donāt worry, practice makes it perfect. Try to solve simple problems first, that will build your confidence. Everything is hard at first, there is a saying āEvery pro was once an amateurā