-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentify_array_levels.js
68 lines (55 loc) · 1.58 KB
/
identify_array_levels.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
PROBLEM
Given an array, identify how many nested levels has the array and return the number of levels
input: nested array
output: number
examples
- [1, [2]] // 2
- [1, [2], [3]] // 2
- [1, [2, [3]], [2]] // 3
ALGORITHM
declare a variable levels and set it to 1
declare a fucntion countLevels that receives an array "arr" as a param
declare a variable newArray and set it to an empty array
decalre a variable addedOne and set it to false (this variable would track if we already added a level to levels when iterating over the elements of the same level)
iterate over each element of arr
if the element at i position is an array, push each element of this array to newArray
if addedOne is false
add one to levels
set addedOne to true
if newArray length is biggger than 0
call counLevels(newArray)
return levels
defien a function resetLevels that set the valuo of levels to 1
call this fucntion after every time countLevels is called
*/
let levels = 1;
function resetLevels() {
levels = 1;
}
function countLevels(arr) {
let newArray = [];
let addedOne = false;
arr.forEach(e => {
if (Array.isArray(e)) {
newArray.push(...e);
if (!addedOne) {
levels +=1
addedOne = true
}
}
});
if (newArray.length > 0) {
countLevels(newArray)
}
return levels
}
console.log(countLevels([1, [2]])); // 2
resetLevels()
console.log(countLevels([1, [2], [3]] )) // 2
resetLevels()
console.log(countLevels([1, [2, [3]], [2]])) // 3
resetLevels()
console.log(countLevels([1, []])) // 2
resetLevels();
console.log(countLevels([[[[[[]]]]]])) // 6