Hello. I have scanned the forum for “sum all primes” and have seen similar questions but I couldn’t solve my problem from those. If the answer is out there, the algorithm’s title is not in the title of the topic. Soz.
What I have is a code that returns a sum that I see in the test section. That would make me think that it works. I have also tried others:
sumPrime(2) returns 2
sumPrime(5) returns 10 …
Help me, Codie Won. You’re my only hope.
function isPrime(val) {
for(var i = 2; i < val; i++) {
if(val % i === 0) {
return false;//is composite and not in the cool prime club
}
}
return val > 1; //start at whole numb two
}
var primes=[];//arr to push to
function sumPrimes(num) {
for (var n = 2; n <= num; n++){//from two to num
if(isPrime(n)){//if passes test in f(x) above, ...
primes.push(n);
}
} var sum = primes.reduce(function(a,b){
return a+b;
}, 0);//standard reduce
return sum;
}
sumPrimes(977);//returns 73156 but i don't pass the test and sumPrimes(10) returns 17.