Sum All Primes feedback on a completed solution

Tell us what’s happening:
Hi guys,

I’d like some feedback on my code. I have in my eyes a very ugly implementation of sieve of eratosthenes. Although I have a successful solution, I’m not happy with this code.

I came from python, I had in my mind a hazy memory of using a booleans and reading the wiki. I ended up pulling up my old python code from last year.

I would appreciate any comments from my fellow peers.

Thanks!

  **Your code so far**

function sumPrimes(num) {
let mymultiples = [1];
let myprimes = [];


for (let i = 2; i <= num; i ++){
  if (!mymultiples.includes(i)){
    mymultiples.push(i);
    myprimes.push(i);
  }
  for (let j = i**2; j <= num + 1; j = j + i){
    if (!mymultiples.includes(j)){
      mymultiples.push(j);
    }
  }
}
console.log(myprimes);
let sum = 0;
for (let i in myprimes){
  sum += myprimes[i];
  
}
console.log(sum);
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/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55

Challenge: Sum All Primes

Link to the challenge:

That much pushing will slow down your code.

To compare, see my solution here

I also wrote the guide solutions if you want a simpler version of the sieve to compare to.

Thanks Jeremy, this helps.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.