-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.js
More file actions
112 lines (99 loc) · 2.35 KB
/
recursion.js
File metadata and controls
112 lines (99 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Displays a countdown from a given number to 0 with a one-second interval.
* @param {number} count - The starting number for the countdown.
*/
function countDown(count) {
console.log(count);
if (count == 0) {
return;
}
setTimeout(() => { countDown(count - 1) }, 1000);
}
/**
* Calculates the sum of digits of a given number.
* @param {number} num - The input number.
* @returns {number} The sum of the digits of the input number.
*/
function digitSum(num) {
if (num == 0) {
return num;
}
return num % 10 + digitSum(Math.floor(num / 10));
}
// var arr = [2, [3, 12, 24], 17, [54, [24, 3], 8]];
// var result = [];
/**
* Flattens a nested array.
* @param {Array} arr - The input array containing nested arrays.
* @param {number} i - Index to traverse the input array.
*/
function flattenArray(arr, i) {
if (i >= arr.length) {
return;
}
if (Array.isArray(arr[i])) {
flattenArray(arr[i], 0);
} else {
result.push(arr[i]);
}
flattenArray(arr, i + 1);
}
/**
* Calculates the factorial of a given number.
* @param {number} i - The input number.
* @returns {number} The factorial of the input number.
*/
function factorial(i) {
if (i > 1) {
return factorial(i - 1) * i;
}
return 1;
}
/**
* Calculates the nth Fibonacci number.
* @param {number} n - The index of the desired Fibonacci number (0-based).
* @returns {number} The nth Fibonacci number.
*/
function findFib(n) {
if (n <= 0) {
return 0;
} else if (n < 3) {
return 1;
}
return findFib(n - 1) + findFib(n - 2);
}
// let arr = [2, 12, 34, 54, 41];
/**
* Find the index of a specific number
* @param {Arr} arr - Array numbers to search
* @param {*} i - index
* @returns
*/
function printArray(arr, i) {
if (i >= arr.length) {
return;
}
console.log(`Element at index ${i} is ${arr[i]}`);
}
// printArray(arr, 0);
let arr = [2, 16, 23, 33, 41, 51, 73];
function findElement(arr, x, low, high) {
let mid = low + high / 2;
if (low > high) {
return -1;
} else {
if (x === arr[mid]) {
return mid;
} else if (x > arr[mid]) {
return findElement(arr, x, mid + 1, high)
}
else {
return findElement(arr, x, low, mid - 1);
}
}
}
function searchElement(arr, x) {
const index = findElement(arr, x, 0, arr.length - 1);
console.log(`${x} is found at index ${index}`)
}
searchElement(arr, 51);