Sum All Primes, right answers, won't pass

Sum All Primes, right answers, won't pass
0

#1

code outputs answer, but won’t register as a pass :confused:

Your code so far

function isPrime(num) { 
  var all = []; 
  for (var count = 0; count <= num; count++){ 
    all.push(count);} 
  
  all = all.filter(function(number) {
    for (var i = 2; i <= Math.sqrt(number); i++) { 
      if (number % i === 0) return false; }
    return true; }); 
 
  return all.slice(2, all.length -0).reduce(function(a, b){ 
    return a + b; 
  });   
} 

isPrime(10);```
firefox


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

**Link to the challenge:**
https://www.freecodecamp.org/challenges/sum-all-primes

#2

The original challenge function name was sumPrimes, so that is what the tests are looking for to test against. Just rename your isPrime function to sumPrimes and all should be good.


#3

Wow! Thank you so much!

I never would have guessed that you can’t change the function name. I rewrote that code so many times, I can’t even remember why I renamed it…

Thank you Thank you Thank you!


#4

You probably had a second function at one point called isPrime that was used in your sumPrimes, but then you combined the two and forgot to rename it. I have done something like that before.


#5

I think that is exactly what I did. :slight_smile:


#6

I looked at your solution again and noticed a tiny change you could make to the following for loop declaration which would simplify your return statement at the end of the function.

for (var count = 0; count <= num; count++){ 

If you change the above 0 to a 2, then your return statement could simplify to the following without the slice.

  return all.reduce(function(a, b){ 
    return a + b; 
  }); 

If you were to use an isPrime function, then you could avoid creating an array of all the numbers 2 thru num and then having to filter out the primes, by just looping through the numbers 2 thru num with the following code. It avoids the extra step up creating an all numbers array and just creates a primes array.

function sumPrimes(num) { 
  function isPrime(number) {
    for (var i = 2; i <= Math.sqrt(number); i++) {
      if (number % i === 0) return false;
    }
    return true;
  }
  
  var primes = []; 
  for (var count = 2; count <= num; count++){ 
    if (isPrime(count)) primes.push(count);
  } 
  return primes.reduce(function(a, b) {return a + b}); 
}

#7

Thank you! That does simplify things. :slight_smile: