Sum All Primes, possible error in fcc console?

Tell us what’s happening:
I get error with this task when inputs above certain number, returns incorrect result:

  1. I’ve tested in chrome console and result from 977 correctly results with 73156, meaning my code should be fine.
  2. The result in FCC console each time returns different result: 8275, 7141, 5117 etc.
    From what I understand, for some unknown reason, the loop breaks before reaching 977 and returns whatever is there. I would guess it would be to limit harmful while loops or smth…

Your code so far


function sumPrimes(num) {
  let i = 0;
  let primesArr = []

  while(i<=num){
    let divides = []
    for( let j=0; j<num; j++){
      if(i%j===0){
        divides.push(true)
      }
    }
    // console.log(i, divides)
    if(divides.length<=2){
      primesArr.push(i)
    }
    i++
  }
  // console.log(primesArr)
  let sumArr = primesArr.reduce((a,b) => a + b, 0)
  console.log(sumArr-1)
  return num;
}

sumPrimes(977);

Your browser information:

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

Link to the challenge:

It sounds like you are running afoul of the FCC infinite loop protection. To prevent campers from accidentally crashing their browsers, FCC use a timeout. It will only run a test for a certain amount of time before it stops. This can result in valid, but slow solutions failing to pass tests. It’s up to you whether you want to skip this challenge as “good enough”. All of the FCC challenges have solutions which are efficient enough not to time out.

a hint if you wish to optimize your code: you’re checking to see if a number has less than or equal to two divisors (shown in the lines divides.push(true) and divides.length<=2. But these two divisors are 1 and itself. So you can reformat the loop to assume that the divisors 1 and itself will always be there, and exit the loop if i % j === 0

Thank you, thats a valid point about timeout! Just that it took me by surprise for a moment, will know for future tasks!

Thank you Isurasani! I will look into that!