new class js -advanced

This commit is contained in:
Philippe Torrel
2026-06-29 11:35:03 +02:00
parent ee8a87bf2a
commit 31c13250b0
104 changed files with 2632 additions and 138 deletions

View File

@@ -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