What type is my error in Sum All Primes

Tell us what’s happening:
Completing the first part of the challenge.
I’m aware that I need to be converting the sieve of Eratosthenes into code in order to solve this AND I thought I was doing this…
my failure proves otherwise.
Therefore, I’d like to know of what type my error is.

Am I making a mathematical error in exercising the sieve OR is it an incorrect application of the sieve into code?
Comments are below to indicate my reasoning.

Your code so far


function sumPrimes(num) {
//begin array with lowest prime  
let array = [2,];
//iterate through array of numbers
  for(let i = 2; i< num; i++){
//if number is odd and if number is not a multiple of the first four primes after 2, then push it into the array
    if(i % 2 !== 0 && i !== i*(3,5,7 || 9)){
      array.push(i)
    }
  } 
  console.log(array)
//sum primes within array together
  return num;
}

sumPrimes(977);
//sieve of eratosthenes says primes can be found for any number greater than 2 by finding the odd numbers and the numbers that are not a multiple of any higher numbers

Your browser information:

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

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

Tell me what do you think this line does:

i*(3,5,7 || 9)

My first thought was to figure out a way to eliminate all higher numbers with multiples including these lower primes within them. Considering your comment, I’m thinking this is incorrect usage of || and that a different operator should be used OR that it should be combined with my check for odd numbers.
So, I’m thinking that it’s primarily my conversion of the sieve into code that is the problem, correct?

What if I say that this line:

i*(3,5,7 || 9)

and this line:

i*('hi', [], 7 || 5555)

does exactly the same?

Correct.

1 Like

No, this doesn’t mean what you think.
Try to add console.log(i * (3,5,7 || 9)) and see what it is.
Also, i will always be different from itself multiplied by something else. So, that will be true no matter what.
Also, why 9? It is not prime and it is already a multiple of 3.

Also you are not counting for numbers that are divisible for the other primes you find later, but not the smaller ones. For example, 143. It is not prime and it is not divisible by 2,3,5,7

I will suggest using the every() method

There are many others way obviously, you don’t need to use this.