Sum All Primes - Code not functional on Firefox

Sum All Primes - Code not functional on Firefox
0.0 0

#1

Tell us what’s happening:
I’m almost there! I spent quite some time trying to figure this out myself and it seems I’ve gotten really far. Currently, it looks like my code works for smaller numbers but I have no clue to what extent. When FCC tests the code with sumPrimes(977), it returns the wrong value. Any help is appreciated. Thank you in advance!

Your code so far


function sumPrimes(num) {
  let res = 0;
  let primes = [2, 3];
  let prime = true;

  //check each number that is less than or equal to argument
  for( let i = 4; i <= num; i++ ){
    prime = true;
    //if number divisible by anything other than 1 or itself, it isn't prime. checking for remainders
    for( let curr = 2; curr < i; curr++ ){
      if( i % curr == 0 ){
        //set prime to false if it isn't prime
        prime = false;
      }
    }

    //if, in the end, the number is still considered prime, push it to the array of prime numbers
    if( prime == true ){
      primes.push( i );
    }
  }

  //Add everything within primes array and return
  console.log( primes, primes.reduce( ( a, b ) => a + b) );
  return primes.reduce( ( a, b ) => a + b );
}

sumPrimes(23);

Your browser information:

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

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


#2

Your code works maybe try using a different browser? although, I don’t see how cross-browser issue can interfere with this one…


#3

Ok thank you I’ll try a different browser.


#4

Your code works for me also. You could try copying your code, resetting the challenge and then pasting it in again.


#5

More than likely your code is timing out or taking too many iterations through some Loop so the FCC Test environment shuts it down early. You might look at how many iterations your algorithm makes for the largest test number


#6

Could it be because the code isn’t optimal? Or would this be a problem with the test cases?


#7

Is it still not running?
It shouldn’t be timeout termination because sumPrime(977) is still very small.


#8

Try searching the forums for other recent post. This is a pretty common problem with that one Challenge and it pops up every few days


#9

I looked at the basic solution in their hints. I commented out and used what they wrote and it worked. It must be my code but I still can’t figure out why it isn’t running.

In the working solution, it is also very similar to mine where they check every number up until the target to make sure it’s prime. The only difference is that they created a new function to do that instead of doing it all in one function.


#10

I’ve tried it on Firefox, and the algorithm seems to return early somewhere in the middle.
I’ve ran it on Chrome, and it almost always passes.

So, yeah it seems the input is too big.


#11

I tried it on explorer but it didn’t even load up FCC lol. Lemme take a look on chrome. A great thanks to anyone helping out in this post!

Update: Works on Google Chrome