-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate-N-dimensional-array.js
51 lines (41 loc) · 1.26 KB
/
Create-N-dimensional-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Little boy Vasya was coding and was playing with arrays. He thought: "Is there any way to generate N-dimensional array"?
Let us help little boy Vasya. Target of this Kata is to create a function, which will generate N-dimensional array.
For simplicity all arrays will have the same length.
Input:
N: number -> depth of outter array, N > 0
size: number -> length of all arrays, size > 0
All inputs will be valid.
On the last (deepest) level we should put a string wich will describe the depth of our array. Example: 'level 2'
Example:
for createNDimensionalArray(2,3) output should be:
[
['level 2', 'level 2', 'level 2'],
['level 2', 'level 2', 'level 2'],
['level 2', 'level 2', 'level 2'],
]
for createNDimensionalArray(3,2) output should be:
[
[
['level 3', 'level 3'],
['level 3', 'level 3'],
],
[
['level 3', 'level 3'],
['level 3', 'level 3'],
],
]
Good luck!
*/
// Answer:
const createNDimensionalArray = (n, size, currentDepth = n) => {
let result = [];
if (currentDepth === 1) {
return new Array(size).fill(`level ${n}`);
}
for (let i = 0; i < size; i++) {
result.push(createNDimensionalArray(n,size, currentDepth - 1))
}
return result
}
// BigO: O(n2)