Help to explain the code

this is someone else’s solution , the only think I don’t understand is the if condition . can someone explain it to me ?

``````  **Your code so far**
``````
``````
function sumPrimes(num) {
var numbers = []
for(let i=2;i<=num;i++){
numbers.push(i)
}
return numbers.filter((values,index,array) => {
for( let j=0;j<index;j++){
if(values % array[j] === 0){
return false
}
}
return true
}).reduce((acc,b)=>{ return acc+b},0)
}

console.log(sumPrimes(10))
``````
``````  **Your browser information:**
``````

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

Challenge: Sum All Primes

What does is it mean ?

The modulus operator ( `%` ) returns the division remainder.

``````if(values % array[j] === 0)
``````

So, the condition specifically describes a scenario where the `remainder` you would get after dividing the `values` by `array[j]` would be equal to `zero`.

I know that.

I want to know, how it’s checking for prime and non prime numbers

Side note: I wouldn’t focus on this solution too much. It is one of the least effecient solutions I’ve seen.

By definition, a prime number has no number less than itself that evenly divides into it (except for 1, which divides evenly into every number).

1 Like

If every integer between two and the integer nearest the square root of our given number doesn’t divide evenly into that given number (if the modulus of every integer is not zero), then that given number is prime.

2 Likes

I’m not actually sure this code gives the right answer. 7 is prime but `7 % 1 === 0`

2 Likes

Thank you for the reminder, i edited my reply. Any number modulo 1 returns 0.

1 Like

The more I look at this code OP found, the less I like it

1 Like

Where I stand , I don’t know anything about time complexity and efficiency .

It’s not just the time complexity (though that is pretty bad). Ok, I ran it and it technically works but…

1. Making an array like this is very slow and it is not needed for this problem.

2. The filter condition checks way too many potential divisors

3. The filter + reduce loops over the data twice, which is slower

1 Like

ohh yeah , iteration(twice) really making it slow

A great place to start gaining an understanding of finding primes might be taking a look at Wikipedia, in the article about “sieve of Eratosthenes”. Good article, and a good introduction to algorithms in general.

2 Likes

My approach is generally worry about coding later, and try to gain an understanding of how to approach the problem in my head, first.

1 Like

But why is this slow ?

This way of making an array adds one element at a time, slowly making the array bigger and bigger. That dynamic reallocation of space for the array is what is very slow.

In this case, since we need all numbers from 2 to `num`, there is actually no reason whatsoever to make the array anyways, as we can just `for (let i = 2; i < num; i++) {....}`.

If JavaScript had a proper range iterator, then this would be different, but unfortunately it does not.

1 Like

then there’s no point to use filter!

1 Like

Right. I like `.filter()` and `.map()`, but it is important to pick the right tool for the right job.

1 Like

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