Question: I am working on something in javascript where I need to create a matrix with dynamic dimensions depending upon an array.

For example, I have an array with `[3, 5, 6]` - now I want to create a matrix dynamically that is 3 x 5 x 6, and in this matrix, all the values are initialized to 0.

I can seem to find a way to do this dynamically depending upon my initial array values. Anyone have any idea?

Answer: 
You can use recursion.

```js
function createMatrix(dimensions) {
  return dimensions.length === 1 ? Array(dimensions[0]).fill(0) : Array.from({
    length: dimensions[0]
  }, () => createMatrix(dimensions.slice(1)));
}
console.log(JSON.stringify(createMatrix([3, 5, 6])));
```
A recursive function that uses `Array.from` will do the trick.

```js
const makeMatrix = (length, ...restDimensionLengths) => Array.from(
  { length },
  () => restDimensionLengths.length ? makeMatrix(...restDimensionLengths) : 0
);

console.log(makeMatrix(2, 3, 5));
```