Sum All Primes - I solved it but it's not working in FCC. It works in JSbin and Repl.it

Sum All Primes - I solved it but it's not working in FCC. It works in JSbin and Repl.it
0

#1

Background

I was trying to solve the Intermediate Algorithm Scripting: Sum All Primes problem. I came up with the code below and it worked in jsbin and repl.it but it’s not being accepted in freecodecamp.

I know it’s not that good. It seems quite a brute way of solving the problem. But this is what I could come up with at my current state.

Please let me know if there is any problem with this.

sumPrimes(977)` should return 73156 part is not working on FCC but it gives correct answer on jsbin or repl.it

Thanks!

Mycode so far


function sumPrimes(num) {
  let prime = [];
  let factors = [];
  for (let i=1; i<=num; i++){
    factors = [];
    for (let j=1; j<=i; j++){
      if (i%j==0){
        factors.push(j);
      }
    }
     if (factors.length<=2){
      prime.push(i);
      }
  }
  prime.shift();
  prime = prime.reduce((a, b) => a+b);  
  
  return prime ;
}

sumPrimes(10);

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

Your algorithm is not efficient enough to not set off the test suite’s infinite loop protection feature. All the challenges can be solved with an efficient algorithm which will not cause infinite loop protection to activate.


#3

So, inefficiency is the reason.I guess I have to find another way then.