Skip to content

Instantly share code, notes, and snippets.

@aldraco
Created February 26, 2015 19:02
Show Gist options
  • Save aldraco/25223627a0b9f224f94d to your computer and use it in GitHub Desktop.
Save aldraco/25223627a0b9f224f94d to your computer and use it in GitHub Desktop.
sum prime numbers bonfire Challenge
function sumPrimes(num) {
//create an array of prime numbers up to num
var primesArray = [];
function isPrime(number) {
//tests for prime
//var index = 2;
var sqrt = Math.sqrt(number);
for (var index=2; index <= sqrt; index++) {
if (number % index === 0) {
return false;
}
}
return true;
}
for (var i=2; i <= num; i++) {
//test isPrime
if (isPrime(i)) {
primesArray.push(i);
}
//insert into an array
}
//sum the array
var sum = primesArray.reduce(function(a,b) {
return a+b;
});
return sum;
}
sumPrimes(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment