Sum All Primes Loop Problems

Tell us what’s happening:
Hey guys, i’m trying to do the prime numbers javascript problem and I cant figure out whats going on. It kept leaving the loop earlier than I thought it was suppost to be, so I commented it out and just put a normal for loop doing console.log on the index. Every time I run it It gives slightly different output even though it should just be giving me count:1, count: 2, … , count: num (or num - 1 I guess technically). I found this online https://github.com/freeCodeCamp/freeCodeCamp/issues/7098 and tried using //noprotect but it didn’t do anything. I feel like I must be missing something here… These two screen shots from two consecutive tests. Thanks for the help!


Your code so far


function sumPrimes(num) {
  console.log ("\n\n\nNEW for :" + num);
  let primeSum = 0;
  let i;
  let j;
  /*for (i = 1; i <= num; i++){
    for (j = i-1; j > 1; j--){
      console.log(i,j);
      if (i % j == 0){
        console.log ("break");
        break;
      }
    }
    if (j == 1){
      console.log("prime: " + i);
      primeSum += i;
    }
    console.log("i: " + i + " num: " + num);
  

  console.log(primeSum);
  */

  for (let k = 1; k < num; k++){
    console.log("count: " + k);
  }

  return primeSum;
}

sumPrimes(10);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:

  for (let k = 1; k < num; k++){
    console.log("count: " + k);
  }

this loop is working fine when I paste your code in my FCC page.
The console shows it counting from 1 to num-1 as expected .

I’m not clear why you think it is not working…

Hmm, did you look at the two screenshots I put up? The console is to the right and you can see the results i’m getting.

I think there are errors on the page (the FCC page) that may be causing the weird console results.

Try using repl.it to debug your code and you will see it loops perfectly

My first thought was that the infinite loop detection was still hitting you somehow, though I’m not at home to test better right now

For what it’s worth I think you should revise your prime number generation, the way you’re using now is terribly inefficient anyway

Consider reading about the sieve of Eratosthenes and trying to implement that

1 Like

Im trying to see if it will run just locally now hopefully that will fix it? I know its not super great method for finding them, I thought about looking up a better one but I decided to try and come up with a solution myself first. The Sieve of Eratosthenes looks a lot more efficient (looking at the wikipedia page now)

Solution: I ran it locally and I need to change the algorithm, but it fixed the problems with the loops leaving themselves too quickly. I guess it must’ve been something with my computer and the fcc page that make it not run correctly.

I just got home and just copied and pasted your code and it worked fine for me too, hrmmm

IIRC FCC’s infinite loop detection works by placing a timer somewhere around the loops (I forget the details)

Can you try with noprotect on the simple case in the OP where it’s just printing? It could just be that your pc is incredibly slow for some reason right now?

Here is the simple loop with no protect (In the picture its above loop and in loop, I also tried putting it just above the loop and also just putting it in the loop). As you can see the loop thats suppose to go to 997 only makes it to 12. Its possible my computer is too slow I guess. It seems like its running decent enough for me, but it is a 7 year old macbook. I also did try restarting chrome but it didn’t help.

Can you turn on timestamps in your console logs? It’d be interesting to see how long they’re taking…

console logs can be a bit of a time hog, but even so you should be able to print 1000 times within whatever FCC’s limit on your machine, surely

Not going to lie, I’m scraping the barrel here

Have you tried it in Firefox and/or Safari?

Here is it with the timestamps on.

And I here it is on firefox. It looks like it gets further, usually into about the low 200s before it gives up. it has some messages on there at the bottom but I don’t really know what they mean.

the messages made me curious, I checked the other tabs in chrome dev tools and there were these. I guess these are confirming that its taking to long and its aborting?

I can’t help but notice Firefox is printing at a much faster rate, explaining the greater number, which leads me to believe somewhat that infinite loop protection is still hitting you even with noprotect

Having said that though, I checked the github freecodecamp repo and it seems the default loopTimeout is 1000 ms which your code doesn’t seem to be even reaching before it ends (at least in chrome, idk about the start time in firefox)

So that’s confusing me a little too

Tagging @camperextraordinaire incase he’s seen this before, sees something obviously wrong and has other ideas, or knows who to ask or report this to

(sorry to rudely summon, but this seems important)

As a last resort, are there any browser extensions you’ve added to both (?!) browsers that could possibly throttle or kill long running scripts?

Thanks for replying Randell, did you manage to reproduce it in some way before then? I haven’t been able to at all, perhaps it’s browser and platform dependent

I’m wondering if it’s still possibly an issue with taking too long due to the logs or an issue with console logs display specifically.

If the latter I guess mystery solved, and we just can’t use so many console logs directly in the FCC page

If the former then idk what to suggest to the OP except to just try the sieve of eratosthenes and hope it’s fast enough (without console logs too ofc)

Thanks again for replying Randell despite being so busy

1 Like

Thank you guys for all the help. I figured out adobe wanted to eat 50 - 90% of my cpu with none of their applications even running, so removing that from my computer maybe helped? And I removed the console log from inside the for loop and just put one on the outside like randells last response. That correctly outputs 997, but only goes to like 20ish with the console logs inside the for loop. So I guess thats good enough, its would be nice to be able to put it in the loop for debugging purposes though. I used Eratosthenes Sieve thanks for the advise gebulmer!

1 Like