I am supposed to write an algorithm that returns the sum of all the primes up to a certain number(argument), including the argument itself. This code seems to be working just fine(I tested it on smaller numbers),however there must be a bug because when I pass 977 as an argument, the programs returns 108789, which is supposedly not correct. According to freecodecamp.org, it should return 73156. I have already checked the array before adding the values but I can't see the problem here.
function sumPrimes(num) {
function isPrime(n){
return ((n/2 === 1 || n/3 === 1 || n/5 === 1 || n/7 === 1)?true:
(n%2===0 || n%3 === 0 || n%5 ===0 || n%7 === 0)?
false:true);
};
let result = [];
let final;
for(let i = 2; i <= num; i++){
if(isPrime(i)){
result.push(i);
}
}
final = result.reduce((x,y) => x + y);
console.log(final); // returns 108789
}
sumPrimes(977);