Tell us what’s happening:
I’m working on “Sum All Primes” Algorithm and have 2 working conditions for sumPrimes(10) but when it comes to sumPrimes(977) the result is way to high. I tried to go step by step over my code but have a trouble understanding what is wrong. Is my condition missing something?
Your code so far
function sumPrimes(num) {
let primeArr = [2, 3, 5];
let sumPrimes = 0;
let condition = false;
// Build prime numbers arr
for(let i = 6; i <= num; i++) {
condition = !(i % 2 === 0 || i % 3 === 0);
if(condition) {
primeArr.push(i);
}
}
// Sum primeArr
for(let y = 0; y < primeArr.length; y++) {
sumPrimes += primeArr[y];
}
return sumPrimes;
}
sumPrimes(977);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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/