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)
}
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)
}
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));