Last active
November 22, 2015 04:00
-
-
Save minsooshin/62f328042a335ea276aa to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Sum All Primes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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