Intermediate Algorithm Scripting: Sum All Primes

Tell us what’s happening:

My code returns 73156 on the function call with 977 but does not pass the test. Can somebody tell me what is the problem?

function sumPrimes(num) {
 let primeNos = [];
 for (let i = 2; i <= num; i++) {
   let counter = 0;
   for (let j = 0; j < i; j++) {
     if (i % j === 0) {
       counter += 1;
      }
    }
    if (counter === 1) {
      primeNos.push(i);
    }
  }
  return primeNos.reduce((a, b) => a + b);
}

sumPrimes(977);

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes/

Hey @devenkshp ,

Your function will have to return the sum whereas you are only console.log ing the result.

See if this helps.

2 Likes

Thanks @aditya_p, but I’ve returned the final value inside the challenge and it still does not work.

Your code looks fine, you must return your result without the console.log. If you can’t pass, please post what you have tried already.

console.log returns undefined. Example:

return console.log(10); // returns undefined
return 10; // returns 10
1 Like

replace ‘console.log’ with ‘return’

1 Like