Sum all Primes JavaScript help

Tell us what’s happening:
I don’t understand why sumPrimes(977) should return 73156 shouldn’t it return 977 since 977 is a prime number. I need help fixing my code so it returns the correct answer

  **Your code so far**

function sumPrimes(num) {
let primes = [];

let count = 1;

while (count < num) {
  let eq = num / count;
  primes.push(eq);
  count++;
}

return primes.filter(c => c % parseInt(c) == 0).reduce((p, c) => p + c);
}

sumPrimes(977);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0.

Challenge: Sum All Primes

Link to the challenge:

You should return the sum of all primes up to 977, so your result will be much bigger than 977.

You need to check every number from 2 to n to see if they are prime. I would not create an array of primes for this.

Mathematics isn’t my strongest suit but isn’t 977 only divisible by 1 and 977

Sure, but 2 is also prime. 3 is also prime. 5 is also prime… You need to sum (add together) every prime between 2 and n (n=977 in one of the test cases).

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.