# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8566025,"math_prob":0.9433646,"size":3227,"snap":"2022-27-2022-33","text_gpt3_token_len":806,"char_repetition_ratio":0.09711449,"word_repetition_ratio":0.0,"special_character_ratio":0.26371244,"punctuation_ratio":0.12922174,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98912746,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-04T06:22:33Z\",\"WARC-Record-ID\":\"<urn:uuid:7ca17cf6-49e0-46c7-8f64-ca20466d7f86>\",\"Content-Length\":\"64555\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2c359f50-ba8b-49b2-97cd-4caed75bc938>\",\"WARC-Concurrent-To\":\"<urn:uuid:a6543720-1e03-4901-bc24-7a72e116e452>\",\"WARC-IP-Address\":\"64.71.144.205\",\"WARC-Target-URI\":\"https://forum.freecodecamp.org/t/help-to-explain-the-code/472007\",\"WARC-Payload-Digest\":\"sha1:4AVJYXU6B67XBI34WEHBRVCA25ZA6DQ4\",\"WARC-Block-Digest\":\"sha1:XRBFUALI5VQZJDMQDX2RZVHAWCWMRVP3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104354651.73_warc_CC-MAIN-20220704050055-20220704080055-00021.warc.gz\"}"}