new class js -advanced
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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"
|
||||
@@ -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)
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user