Sum All Primes Explanation

Sum All Primes Explanation
0

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


#2

@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 :slight_smile:


#3

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.


#4
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 :slight_smile:


#5

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?

Thanks again


#6

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 :slight_smile:


#7

Wow, makes sense now I think. Thanks so much dude, much appreciated.

I guess 4 <= numArgOne, so isPrime(i) is called.
J < numArgTwo(4)
4 % J is equal to 0 so it returns false

Does j return to 2 next time the code runs? E.g. next time isPrime(i) is called?

Forgive me, I did a lot of the lessons before and took a lengthy break from this.


#8

Yes, j always starts from the 2 :slight_smile: That is the way you wrote the code


#9

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


#10

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:slight_smile:


#11

Thanks for all the help and inspiration!