Skip to content

Instantly share code, notes, and snippets.

@clive-bunting
Created December 5, 2015 15:07
Show Gist options
  • Select an option

  • Save clive-bunting/5a209d8ba752bab19b7e to your computer and use it in GitHub Desktop.

Select an option

Save clive-bunting/5a209d8ba752bab19b7e to your computer and use it in GitHub Desktop.
Bonfire: Sum All Primes
// Bonfire: Sum All Primes
// Author: @clive-bunting
// 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 = [];
for (var trial = 2; trial <= num; trial++) {
var isPrime = true;
for (var prime = 0; prime < primes.length; prime++) {
if (trial % primes[prime] === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(trial);
}
}
return primes.reduce(function(a,b) {return a + b;});
}
sumPrimes(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment