This solution is very basic and simple, however I don’t think its the most efficient one, is it the most efficient one? if not then please write down the most efficient one. By the way my code looks pretty messy so any tips for making it look cleaner will be appriciated (rewriting it so it will look better is good)
My code so far
function sumPrimes(num) {
var nums = [];
//an array that contains all numbers from 2-num (1 isnt
//prime either divisble)
for (let i = 2; i <= num; i++) {nums.push(i)}
//filters the array by checkung if any number there is divisble by another number that's not the same
return nums.filter(function(val) {
for (let j = 2; j <= num; j++) {
if (val % j === 0 && j !== val) {return false}
}
return true
})
//sums all the numbers of the filtered array
.reduce((sum, a) => sum + a)
}
sumPrimes(10);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Sum All Primes
Link to the challenge: