Skip to content

Instantly share code, notes, and snippets.

@minsooshin
Last active November 22, 2015 04:00
Show Gist options
  • Save minsooshin/62f328042a335ea276aa to your computer and use it in GitHub Desktop.
Save minsooshin/62f328042a335ea276aa to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Sum All Primes
// Bonfire: Sum All Primes
// Author: @minsooshin
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-primes
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function sumPrimes(num) {
var primes = [2];
function isPrime(n) {
for (var i = 2; i < n; i++) {
if ( n % i === 0 ) return false;
}
return true;
}
for ( var i = 3; i <= num; i+=2 ) {
if (isPrime(i)) primes.push(i);
}
return primes.reduce(function(sum, curr) {
return sum + curr;
});
}
sumPrimes(977);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment