Skip to content

Instantly share code, notes, and snippets.

@jepras
Created October 2, 2018 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepras/81c2fb1d36ec57c54537a99badd4d685 to your computer and use it in GitHub Desktop.
Save jepras/81c2fb1d36ec57c54537a99badd4d685 to your computer and use it in GitHub Desktop.
Use a isPrime method to return true on primes. Then loop through and add result to output if isPrime.
function sumPrimes(num) {
function isPrime(num) {
if(num < 2) return false;
for (var i = 2; i < num; i++) {
if(num%i==0)
return false;
}
return true;
}
var output = 0;
for(var i = 0; i <= num; i++){
if(isPrime(i)) {
output =+ output + i;
}
}
return output;
}
sumPrimes(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment