new class js -advanced
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
function findDeepestLevel(arr, currentLevel = 1) {
|
||||
// Base case: when the array is empty, return the current level
|
||||
if (arr.length === 0) {
|
||||
return currentLevel;
|
||||
}
|
||||
|
||||
let maxLevel = currentLevel;
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (Array.isArray(arr[i])) {
|
||||
// Recursive case: if the element is an array, call the function recursively
|
||||
const depth = findDeepestLevel(arr[i], currentLevel + 1);
|
||||
if (depth > maxLevel) {
|
||||
maxLevel = depth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxLevel;
|
||||
}
|
||||
|
||||
// Test cases
|
||||
console.log(findDeepestLevel([1, [2, [3, [4]], 5]])); // => 4
|
||||
console.log(findDeepestLevel([1, 2, 3, 4, 5])); // => 1
|
||||
console.log(findDeepestLevel([])); // => 1
|
||||
console.log(findDeepestLevel([1, [2], [3, [4, [5]]]])); // => 4
|
||||
console.log(findDeepestLevel([1, [2, [3]], [4, [5, [6]]]])); // => 4
|
||||
Reference in New Issue
Block a user