-4

I will attach image of challenge solution that i cannot understand this solution not the challenge itself

Code :

function array(num,length){
  return [...Array(length).map((_,i) => num * (i+1)
}
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • Have you tried looking up the documentation before asking? [`...`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Spread_syntax), [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/Array#parameters), [`map`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/map). This is very straight-forward (if the syntax errors were fixed). – Sebastian Simon Dec 12 '21 at 16:55

1 Answers1

0

First of all there are something wrong with the syntax: There is both a missing ] and ).

There is a function called "array" (maybe it is not the best name...). It takes two parameters: a number (num) and a length (length), both numbers.

length is used for defining and array (Array(length)). So length=4 will result in an array: [undefined,undefined,undefined,undefined]. map() is a function that takes an array and returns an array with a similar length. The array returned by map() contains numbers that are calulated based on the index (the i) and the parameter num, so first 0*3, then 1*3, then 2*3. The array is then returned as the "result" of calling the function "array".

Here I print out the array in the console:

function array(num, length){
  return [...Array(length)].map((_,i) => num * (i+1))
}

console.log(array(3, 4));
chrwahl
  • 8,675
  • 2
  • 20
  • 30