Tell us what’s happening:
I have a question about part of the code below, can someone explain it to me? Thank you.
Your code so far
function sumPrimes(num) {
// step 1
let arr = Array.from({length: num+1}, (v, k) => k).slice(2);
// step 2
let onlyPrimes = arr.filter( (n) => {
let m = n-1;
while (m > 1 && m >= Math.sqrt(n)) {
if ((n % m) === 0)
return false;
m--;
}
return true;
});
// step 3
return onlyPrimes.reduce((a,b) => a+b);
}
// test here
sumPrimes(977);
I understood the Array.from method and the slice() method. But I have not seen any code like this:
({length: num+1}, (v, k) => k)
I have never met it before. What is it? What is its function? In the above case, what will the result be?
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes/