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,19 @@
function combinations(n, k) {
// Base Cases
if (k === 0 || k === n) {
return 1;
}
// If k is greater than n, there are no combinations
if (k > n) {
return 0;
}
// Recursive Cases
return combinations(n - 1, k - 1) + combinations(n - 1, k);
}
// Test Cases
console.log(combinations(5, 2)); // => 10
console.log(combinations(6, 3)); // => 20
console.log(combinations(4, 0)); // => 1
console.log(combinations(4, 4)); // => 1
console.log(combinations(5, 6)); // => 0

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

View File

@@ -0,0 +1,19 @@
function findMax(arr) {
// Base case: empty array
if (arr.length === 0) {
return null;
}
// Base case: array with one element
if (arr.length === 1) {
return arr[0];
}
// Recursive case: return the maximum of the first element and the maximum of the rest of the array
const subMax = findMax(arr.slice(1));
return arr[0] > subMax ? arr[0] : subMax;
}
// Test cases
console.log(findMax([1, 5, 3, 9, 2])); // => 9
console.log(findMax([-10, -20, -3, -4])); // => -3
console.log(findMax([42])); // => 42
console.log(findMax([])); // => null

View File

@@ -0,0 +1,14 @@
function reverseString(str) {
// Base case: if the string has only one character, return it
if (str.length <= 1) {
return str;
}
// Recursive case: return the last character of the string and call the function with the rest of the string
return reverseString(str.slice(1)) + str[0];
}
// Test cases
console.log(reverseString('hello')); // => "olleh"
console.log(reverseString('Recursion')); // => "noisruceR"
console.log(reverseString('')); // => ""
console.log(reverseString('A')); // => "A"

View File

@@ -0,0 +1,26 @@
function sumNestedArray(arr) {
// Base case: when the array is empty, return 0
if (arr.length === 0) {
return 0;
}
let sum = 0;
for (let element of arr) {
if (Array.isArray(element)) {
// Recursive case: if the element is an array, sum its elements
sum += sumNestedArray(element);
} else if (typeof element === 'number') {
sum += element;
}
}
return sum;
}
// Test cases
console.log(sumNestedArray([1, [2, [3, 4], 5], 6])); // => 21
console.log(sumNestedArray([[[[1]]], 2, [3, [4, [5]]]])); // => 15
console.log(sumNestedArray([])); // => 0
console.log(sumNestedArray([10, [20, [30, [40]]]])); // => 100
console.log(sumNestedArray([1, 'a', [2, 'b', [3, 4]], 5])); // => 15 (ignores non-numbers)

View File

@@ -0,0 +1,12 @@
function sumRecursive(n) {
if (n === 0) {
return 0;
}
return n + sumRecursive(n - 1);
}
// Test Cases
console.log(sumRecursive(5)); // => 15 (5 + 4 + 3 + 2 + 1)
console.log(sumRecursive(10)); // => 55 (10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1)
console.log(sumRecursive(0)); // => 0