Skip to main content
Commonmark migration
Source Link

Reason For Anwser

#Reason For Anwser ThereThere 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.

#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.

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.

added 2 characters in body
Source Link
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 (currDepthcurrDepth+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.

#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 === 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.

#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.

Source Link
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 === 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.