Tell us what’s happening:
Hello, I can’t seem to understand what’s wrong with this code.
sumPrimes(10) returns the correct result but everything further than that doesn’t work
Your code so far
function sumPrimes(num) {
var primeSum =0;
var sqrtI = 0;
var primeArray = [];
function add(a,b){
return a+b;
}
for (i=2;i<=num;i++){
primeArray.push(i);
sqrtI = Math.sqrt(i);
for (k=2;k<=sqrtI;k++){
if (i % k === 0){
primeArray.pop(i);
}
}
}
return primeArray.reduce(add,0);
}
sumPrimes(20);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/sum-all-primes