Sum All Primes - can't get last test

Sum All Primes - can't get last test
0

#1

Tell us what’s happening:

Not sure where this is going wrong. My primeArray seems to only contain prime numbers, and I’ve got sumPrimes(10) working fine. Somehow I’m not getting up to the right total with the last challenge.

Your code so far


function sumPrimes(num) {
  let total = 0;
  let primeArray = [];
  function isPrime(candidate){
    for(let i = 2; i < candidate; i++){
      if(candidate % i === 0){
        return false;
      }
    }
    primeArray.push(candidate);
    return true;
  }
  for(let j = 2; j < num; j++){
    isPrime(j);
  }
  console.log(primeArray);
  total = primeArray.reduce((a,b) => a + b);
  console.log(total);
  return total;
}

sumPrimes(10);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) 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

Your prime generation algorithm isn’t fast enough and is tripping the infinite loop protection for the final challenge

I wish we had a note about it in the challenge text, or a console message that it’s occurred when it’s tripped (either change could be a good pull request for a beginner)

Consider googling and reading about various prime number generation methods - a particular favourite of mine is the sieve of eratosthenes