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