## JavaScript - Sum of All Prime Numbers

A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2, and 4.

Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.

Solution 1 Basic approach

`function sumPrimes(num) {    let i = 1;    let sum = 0;    while (i <= num) {        if (isPrime(i)) {            sum += i;        }        i++;    }    return sum;}//function to check if a number is prime or notfunction isPrime(x) {    for (let i = 2; i < x; i++) {        if (x % i === 0) return false;    }    return x !== 1 && x !== 0;}console.log(sumPrimes(7));`

Output:

`17`

Solution 2 using reduce

`function sumPrimes(max) {    var store = [], i, j, primes = [];    for (i = 2; i <= max; ++i) {        if (!store[i]) {            primes.push(i);            for (j = i << 1; j <= max; j += i) {                store[j] = true;            }        }    }    return primes.reduce((a, b) => a + b);}console.log(sumPrimes(10));`

Output:

`17`
{"ft_lang_label":"__label__en","ft_lang_prob":0.6312204,"math_prob":0.9996431,"size":996,"snap":"2022-27-2022-33","text_gpt3_token_len":320,"char_repetition_ratio":0.13205644,"word_repetition_ratio":0.0,"special_character_ratio":0.3815261,"punctuation_ratio":0.1838565,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999354,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-01T17:15:16Z\",\"WARC-Record-ID\":\"<urn:uuid:03a9d989-52be-4600-8abe-9f9974d93f6d>\",\"Content-Length\":\"29709\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:93e9fc7d-ed0a-4048-a82d-2286a1113535>\",\"WARC-Concurrent-To\":\"<urn:uuid:db630049-e51a-4df2-a45f-18f8c4614b7d>\",\"WARC-IP-Address\":\"172.67.138.254\",\"WARC-Target-URI\":\"https://www.codesadda.com/example/javascript/JavaScript%20-%20Sum%20of%20All%20Prime%20Numbers/\",\"WARC-Payload-Digest\":\"sha1:63DR4GHPZ4A7PSGUO5FCLR7B7JUX2POK\",\"WARC-Block-Digest\":\"sha1:3XOYOCTVHBAAOKBJ3F64YHT4B4JTLS4G\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103943339.53_warc_CC-MAIN-20220701155803-20220701185803-00691.warc.gz\"}"}