I'm tring to shake this lint error

Sum All Primes CodePen.filter

I am following Mr. Snow’s tutorial and he mentions we could probably refactor the code to use .filter instead of a for loop. I think I must be getting close, because the console.log(prime); actually has the desired value and I think I could even delete the return prime.reduce on line 21, I’ve even tried moving different versions of return prime up to line 16… but the more I try to shake this lint error, I start getting .reduce on an undefined array, so I’ve tried adding 1,2 or 3 as a starting value to the starting array… the more I try to work around this bug, the more I tend to be breaking everything else…

function sumPrimes(num) {
  //create array
  var prime = [];

prime = prime.filter( (n) => { 
    let m = n-1;
    while (m > 1 && m >= Math.sqrt(n)) { 								

    if ((n % m) === 0) 
        return false;
        m--;
    }
		 console.log(prime);
      return true;

  }

  //reduce to add numbers in array
  return prime.reduce(function(a, b) {
    return a + b;
  });
  
}

sumPrimes(10); // should return a number.
sumPrimes(10); // should return 17.
sumPrimes(977); // should return 73156.

original tutorial, by Mr. Snow

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