Exercise : Write a function that when given a number >= 0, returns an Array of ascending length subarrays. The subarrays should be filled with 1s.
How it should work :
pyramid(0) => [ ]
pyramid(1) => [ [1] ]
pyramid(2) => [ [1], [1, 1] ]
pyramid(3) => [ [1], [1, 1], [1, 1, 1] ]
This is the solution that I found online:
function pyramid(n) {
const res = [];
for(let i = 0; i < n; i++){
res.push([...Array(i+1)].fill(1))
}
return res;
}
The solution works pretty well, but I really can't understand this line of code :
res.push([...Array(i+1)].fill(1))
I know how the methods push and fill work, but what [...Array(i+1)]
means?