Sum All Primes (I can't find my mistakes)

Sum All Primes (I can't find my mistakes)
0.0 0

#1

Tell us what’s happening:
I wrote this code to solve but it seems to have something wrong

Your code so far

function sumPrimes(num) {
    numArray = [];
    var sum = 0;

    function isPrime(num) {
        for (var i = 2; i < num; i++) {
            if (num % i === 0) {
                return false;
            }
        }
        return num;
    }

    for (var i = 2; i < num; i++) {
        if (isPrime(i)) {
            numArray.push(i);
        }
    }


    for (var j = 0; j < numArray.length; j++) {
        sum += numArray[j];
    }
    return sum;
}


console.log(sumPrimes(10)); // should return 17 and it does :smile:
console.log(sumPrimes(977)); // should return 73156. But it returns 72179 :sleepy:


#2

The instructions state “Sum all the prime numbers up to and including the provided number”. Your code only sums all the prime number up to and excluding the provided number. You just need to change a < to a <=


#3

I’m having trouble understanding why sumPrimes(10) should return 17. Shouldn’t it return 27 instead? Because the array of primes is: [ 2, 3, 5, 7, 10 ]. Am I missing something here?


#4

10 is not prime, because it can be divided by 2 and 5.


#5

oh God of course! sorry about that dumb question!