Tell us what’s happening:
Not sure where this is going wrong. My primeArray seems to only contain prime numbers, and I’ve got sumPrimes(10) working fine. Somehow I’m not getting up to the right total with the last challenge.
Your code so far
function sumPrimes(num) {
let total = 0;
let primeArray = [];
function isPrime(candidate){
for(let i = 2; i < candidate; i++){
if(candidate % i === 0){
return false;
}
}
primeArray.push(candidate);
return true;
}
for(let j = 2; j < num; j++){
isPrime(j);
}
console.log(primeArray);
total = primeArray.reduce((a,b) => a + b);
console.log(total);
return total;
}
sumPrimes(10);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes