Created
October 10, 2017 16:41
-
-
Save jwill9999/7ae75a884ae89e1dc716f594ff6138a0 to your computer and use it in GitHub Desktop.
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
| /* | |
| Sum all the prime numbers up to and including the provided number. | |
| A prime number is defined as a number greater than one and having only two divisors, one and itself. For example, 2 is a prime number because it's only divisible by one and two. | |
| The provided number may not be a prime. | |
| */ | |
| function sumPrimes(num) { | |
| var primes = []; | |
| for (var j = 2; j <= num; j++) { | |
| if (isPrimeNumber(j)) { | |
| primes.push(j); | |
| } | |
| } | |
| function isPrimeNumber(num) { | |
| for (var i = 2; i < num; i++) { | |
| if (num % i === 0) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| console.log(primes); | |
| return primes.reduce(function(a, b) { | |
| return a + b; | |
| }); | |
| } | |
| sumPrimes(977); | |
| /* | |
| test your code here | |
| sumPrimes(10) should return a number. | |
| sumPrimes(10) should return 17. | |
| sumPrimes(977) should return 73156. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment