Skip to main content
2 of 3
added 2 characters in body
Michael Warner
  • 4.4k
  • 3
  • 25
  • 50

#Reason For Anwser There are good answers here but as JavaScript has changed here is an additional method of tackling this problem with some of the updated features in JavaScript.

function nArray (dem, size=dem, fill=null, currDepth=0) { const arr = new Array(size).fill(fill); return (currDepth+1 === dem) ? arr : arr.map(i => nArray(dem, size, fill, currDepth+1)); }; 

Notes

dem is the dimensions of the array.

size is the size of each dimension by default it is the dem value.

fill is the value that will be the default filled value.

currDepth is not to be used it is for the recursive nature of the function.

Michael Warner
  • 4.4k
  • 3
  • 25
  • 50