Intermediate Algorithm Scripting - Sum All Primes

Tell us what’s happening:
Describe your issue in detail here.

I was going to go the route of “Sieve of Eratosthenes” with this one. The logic I was going for was as follows:

  1. create a range variable and set it equal to an empty array

  2. do a while loop where it generates a range of numbers from the number passed into the function

  3. then I was going to filter through the function to get an array that removed the multiples of 2, 3, 5, and 7, while still keeping the natural/prime numbers themselves

  4. finally, I was going to return that filtered array, reduced with the sum of all the prime numbers that was supposed to be left in the array

My problem starts at the “filtered” variable…it is not filtering based on the conditional statement I placed inside of the filter function, and I’m not quite sure why it’s not working…do I need to chain filter functions with each filtering condition in each one? Or is there something wrong with how I’m writing the conditional statements???

Your code so far

function sumPrimes(num) {

  // create range array from the number passed in
  let range = [];
  while (num > 2) {
    range.push(num - 1);
    num--;
  }

  // create a variable that filters the range array for all numbers that are NOT multiples of 2, 3, 5, or 7
  let filtered = range.filter(n => {
    if (n % 2 !== 0  && n !== 2) {
      return n;
    } else if (n % 3 !== 0 && n !== 3) {
      return n;
    } else if (n % 5 !== 0 && n !== 5) {
      return n;
    } else if (n % 7 !== 0 && n !== 7) {
      return n;
    } else {
      return n;
    }
  });

console.log(range);
console.log(filtered);

  return num;
}

sumPrimes(10);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0

Challenge: Intermediate Algorithm Scripting - Sum All Primes

Link to the challenge:

The code from this guide ran using one of three solutions worked, you were probably looking for more info, the only other time I ran across this was on CodeWars.com
I hope this helps.

This isn’t quite how the Sieve of Eratosthenes works. There are non-prime numbers that aren’t multiples of 2, 3, 5, or 7. Take 121 for example.

You are always returning n, and n is always truthy, so nothing is filtered out here. It would be really hard to make a Sieve of Erathosthenes work with a filter though. You need a nested loop and an array of boolean values.

ahhh, okay, I kind of get what you mean. I’ll have to approach the logic differently. Thanks!

Why include even numbers here? All prime numbers are odd.

Remember, the filter callback method should return either true or false (that’s how filter knows whether to keep the item in the array). You are always returning n, and since n is a number greater than 0 then you are basically always returning true.

P.S. I see that @JeremyLT posted in here before me and we made the same point about returning n. He gave you more info about how the Sieve works as well. I’d just listen to him.

The big idea is roughly

const isPrime = Array(num).fill(true);

// some logic to flag non-prime numbers

console.log(isPrime[2]);  // true
console.log(isPrime[5]);  // true
console.log(isPrime[8]);  // false
console.log(isPrime[12]); // false

This way, you have a big old array that holds boolean values indicating if each number is prime or not, but this array is relatively cheap to make compared to individually checking if each number is prime or not.

cool, I didn’t think that I was doing an extra step here. I could have filtered the even numbers in the while loop initially instead of filtering them out of a range array…I’ll go take a look at the post too, thanks!

thanks! I’ll try that!

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