Update index.md (#22622)
more easy to understand and short than all other solutions
This commit is contained in:
committed by
Christopher McCormack
parent
6b961cd407
commit
9fb1d9f49c
@ -132,6 +132,25 @@ This problem is hard if you have to create your own code to check for primes, so
|
||||
|
||||
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Recursion' target='_blank' rel='nofollow'>Functions - Recursion</a>
|
||||
|
||||
##  Intermediate Code Solution 2:
|
||||
|
||||
function sumPrimes(num) {
|
||||
let nums = Array.from({length: num + 1}).map((_, i) => i).slice(2);
|
||||
for (let n in nums) {
|
||||
nums = nums.filter(val => val == nums[n] || val % nums[n] != 0);
|
||||
}
|
||||
return nums.reduce((prevSum, cur) => prevSum + cur);
|
||||
}
|
||||
// test here
|
||||
sumPrimes(13);
|
||||
|
||||
 <a href='https://repl.it/repls/FrigidFantasticFossil' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### Code Explanation:
|
||||
|
||||
* Use `Array.from()` to generate a sequence of numbers up to and including `num`. Combine with `.slice()` to slice off first two indices `[0, 1]` since all prime numbers must be greater than 1.
|
||||
* If a number is not prime, it is divided by number > 1 other smaller than himself.
|
||||
|
||||
##  Advanced Code Solution:
|
||||
|
||||
function sumPrimes(num) {
|
||||
|
Reference in New Issue
Block a user