Sum All Primes Challenge: Right Values, does not pass the test

Sum All Primes Challenge: Right Values, does not pass the test
0

#1

Tell us what’s happening:

Using another program, I ran the code, and it appears to return the right values (10 returns 17, 977 returns 73156) but the FCC tests say that sumPrimes(977) does not return the right value.

Any help appreciated

Your code so far


function sumPrimes(num) {
  let sum=0;
  let index =2;
  while(index<=num)
  {
    let isPrime=true;
    for(let i=2;i<index;i++)
    {
      if(index % i ==0)
      {
        isPrime=false;
      }
    }
    if(isPrime==true)
    {
      sum+=index;
    }
    index++;
  }
  return sum;
  console.log(sum);
}

sumPrimes(10);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

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


#2

your code passes for me (I’m on chrome) , perhaps this is a browser issue ?


#3

Your algorithm will be too slow, so the tests will be timing out. At the minute you’re doing a lot of unnecessary calculation (for starters, you’re checking even numbers). You’re going through every number multiple times, and that’ll work for small numbers, but as they get bigger the number of calculations your code needs to do just explodes.

It will work locally because your system isn’t designed to time out after a few milliseconds.