Sum All Primes need help

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/

They are the arguments for the Array.from call

The first argument doesn’t need to be an array, it can be anything ‘array-like’, quoting from the docs:

array-like objects (objects with a length property and indexed elements) or iterable objects (objects where you can get its elements, such as Map and Set.

The second, optional, argument is a mapping function, quoting again from the docs:

Array.from() has an optional parameter mapFn , which allows you to execute a map function on each element of the array (or subclass object) that is being created. More clearly, Array.from(obj, mapFn, thisArg) has the same result as Array.from(obj).map(mapFn, thisArg) , except that it does not create an intermediate array.

Docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

1 Like

I got it, thank you.