Getting inconsistent output for a large number like 977

Getting inconsistent output for a large number like 977
0

Faulty with large numbers.

Your code so far


function sumPrimes(num) {
  let sum = 0;
  let prhyme = [];

  for (let i = 2; i<=num; i++){
    let flag = 0;
    for(let j = 1; j<=i; j++){
      if(i%j === 0){
        flag++;
      }
    }
    if(flag === 2){
      prhyme.push(i);
    }
  }

  sum = prhyme.reduce((x1, x2) => (x1+x2));

  return sum;
}

sumPrimes(10);

Your browser information:

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

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

FreeCodeCamp editor has an infinite loop protection that stops loops from running if they take too much time. It seems your code is being stopped by this, you will need to refactor your code so that your loops will need less time. Consider how many iterations are needed to reach a number like that, around 73k. That’s too many.

Thanks! Infinite Loop protection makes sense