# Doubts about a solution : Sum All Primes SPOILER

**URL:** <https://forum.freecodecamp.org/t/doubts-about-a-solution-sum-all-primes-spoiler/217497>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [August 13, 2018, 8:40pm UTC](https://forum.freecodecamp.org/t/doubts-about-a-solution-sum-all-primes-spoiler/217497 "2018-08-13T20:40:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mxlt](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mxlt/32/96522_2.png) [@Mxlt](https://forum.freecodecamp.org/u/Mxlt)\
**Post date:** [August 13, 2018, 8:40pm UTC](https://forum.freecodecamp.org/t/doubts-about-a-solution-sum-all-primes-spoiler/217497/1 "2018-08-13T20:40:08Z")

</div>

Hi,

I got to solve this problem after a while, but I realized that I am repeating myself to much ( I think). So I was trying to find out how others solved the problem. In the guide, there is a basic, intermediate and advanced solution. But there are things I don’t understand. I would appreciate if someone could explain them for me.

This is my solution:

```auto
function sumPrimes(num) {
  var newArray = [];
  for(var i = 2; i <= num; i++){
    if(i == 2){
      newArray.push(i);
    } else if( i % 2 != 0){
        var counter = 0;
        var j = 3;
        if(i == 3){
          newArray.push(i);
        } else {
          while(counter == 0 && j < i){
            if(i % j == 0){
              counter++;
            } else {
              j = j + 2;
            }
          }
            if(counter == 0){
              newArray.push(i);
            }
          
        }
    }
  }
  var sum = newArray.reduce(function(a,b){return a + b;})
  return sum;
}

sumPrimes(10);

```

This is the basic solution on the guide:

```auto
function sumPrimes(num) {
  var res = 0;

  // Function to get the primes up to max in an array
  function getPrimes(max) {
    var sieve = [];
    var i;
    var j;
    var primes = [];
    for (i = 2; i <= max; ++i) {
      if (!sieve[i]) {
        // i has not been marked -- it is prime
        primes.push(i);
        for (j = i << 1; j <= max; j += i) {
          sieve[j] = true;
        }
      }
    }

    return primes;
  }

  // Add the primes
  var primes = getPrimes(num);
  for (var p = 0; p < primes.length; p++) {
    res += primes[p];
  }

  return res;
}

// test here
sumPrimes(10);

```

I am not understanding at all, why the sieve array is initiated, but nothing is ever “pushed” into the array, I mean, the array is empty the whole time, isn’t it?

what is the code trying to do with:

`if (!sieve[i]){...}`

Also, why does the code use a bitwise operator?

`for (j = i << 1; j <= max; j += i)`

[The exercise](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes/)  
[The guide](https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-sum-all-primes/16085)

I appreciate any help.

edit: I appreciate if anybody could explain me how to blur things up. I have tried to blur the solutions but they don’t get blurred, but everythings becomes weird.

---

<div class="post-metadata">

**Author:** ![gignu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gignu/32/128274_2.png) [@gignu](https://forum.freecodecamp.org/u/gignu)\
**Post date:** [January 30, 2020, 12:10pm UTC](https://forum.freecodecamp.org/t/doubts-about-a-solution-sum-all-primes-spoiler/217497/3 "2020-01-30T12:10:44Z")

</div>

I’d like to know that too! Why is no one else wondering about this? Is there something obvious I’m missing?  
Btw, did you find out how it works or could someone explain?

---

<div class="post-metadata">

**Author:** ![gignu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gignu/32/128274_2.png) [@gignu](https://forum.freecodecamp.org/u/gignu)\
**Post date:** [January 30, 2020, 12:34pm UTC](https://forum.freecodecamp.org/t/doubts-about-a-solution-sum-all-primes-spoiler/217497/4 "2020-01-30T12:34:11Z")

</div>

Here a good explanation on shift operators (\<\<) that helped me understand it better:

> **[Please explain Shift Operator | Codecademy](https://www.codecademy.com/forum_questions/558e6874e39efe4c190003fa)**
>
> Please explain Shift Operator

---

<div class="post-metadata">

**Author:** ![gignu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gignu/32/128274_2.png) [@gignu](https://forum.freecodecamp.org/u/gignu)\
**Post date:** [January 30, 2020, 3:37pm UTC](https://forum.freecodecamp.org/t/doubts-about-a-solution-sum-all-primes-spoiler/217497/5 "2020-01-30T15:37:58Z")

</div>

j = i \<\< 1 is basically just a complicated way of saying j = i \* 2.  
It does the exact same thing!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:35am UTC](https://forum.freecodecamp.org/t/doubts-about-a-solution-sum-all-primes-spoiler/217497/6 "2021-02-02T11:35:28Z")

</div>

Closed on February 2, 2021, 11:35am UTC


