Sum All Primes - Problem with big numbers

Sum All Primes - Problem with big numbers
0

#1

Tell us what’s happening:
I’m working on “Sum All Primes” Algorithm and have 2 working conditions for sumPrimes(10) but when it comes to sumPrimes(977) the result is way to high. I tried to go step by step over my code but have a trouble understanding what is wrong. Is my condition missing something?

Your code so far


function sumPrimes(num) {
  let primeArr = [2, 3, 5];
  let sumPrimes = 0;
  let condition = false;
  // Build prime numbers arr
  for(let i = 6; i <= num; i++) {
    condition = !(i % 2 === 0 || i % 3 === 0);
    if(condition) {
      primeArr.push(i);
    }
  }
  // Sum primeArr
  for(let y = 0; y < primeArr.length; y++) {
    sumPrimes += primeArr[y];
  }
  return sumPrimes;
}

sumPrimes(977);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes/


#2

You are putting a number in primeArr if it is not divisible by 2 or 3. That is not the definition of a prime number.


#3

For unknown reason I fixated on the condition beeing a short version of prime number validation but it’s not obviously. I added the function to validate is the number is prime indeed and then put the result of that validation to condition variable for each number lower or equal to num. Thanks for help.