Created
April 6, 2020 14:37
-
-
Save nichanank/8c099f79b505864c48e2fb3c01ae798b to your computer and use it in GitHub Desktop.
This file contains 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
// from FCC | |
function sumPrimes(num) { | |
var numberList = [] | |
for (var i = 2; i <= num; i++) { | |
numberList.push(i) | |
} | |
let nonPrimes = [] | |
for (var i = 0; i < numberList.length; i++) { | |
var p = numberList[i] | |
for (var j = p; j <= num; j+=p) { | |
if (j % p === 0 && j !== p) { | |
nonPrimes.push(j) | |
} | |
} | |
} | |
let primes = numberList.filter(x => !nonPrimes.includes(x)) | |
return primes.reduce((sum,prime) => sum + prime, 0) | |
} | |
sumPrimes(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment