Created
December 5, 2015 15:07
-
-
Save clive-bunting/5a209d8ba752bab19b7e to your computer and use it in GitHub Desktop.
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: @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