-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINTER - FUNCTION - implementing a recursive algorithm.js
More file actions
78 lines (62 loc) · 1.68 KB
/
INTER - FUNCTION - implementing a recursive algorithm.js
File metadata and controls
78 lines (62 loc) · 1.68 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
/*
!!INTER - FUNCTION - IMPLEMENTING A RECURSIVE ALGORITHM!!
> want to implement a function that will recursively traverse an array and return a
string of the array element values, in reverse order
*/
/*
SOLUTION:
+) Use a function literal recursively until the end goal is met
*/
+)
var reverseArray = function(x, indx, str) {
return indx = 0 ? str : reverseArray(x,--indx,(str+= " " + x[indx]));
}
var arr = ['apple','orange','peach','lime'];
var str = reverseArray(arr, arr.length, "");
console.log(str);
var arr2 = ['car','boat','sun','computer'];
str = reverseArray(arr2, arr2.length, "");
console.log(str);
/*
CONCEPT OF RECURSION
1) An example of recursion in mathematics is the Fibonacci Sequence
>> Fibonacci number is the sum of the two previous Fibonacci numbers
2) example of JavaScript recursion is the solution for a Fibonacci
3) or a factorial
*/
1)
f(n)= f(n-1) + f(n-2),
for n= 2,3,4,...,n and
f(0) = 0 and f(1) = 1
2)
var fibonacci = function(n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
3)
function factorial(n) {
return n == 1 ? 1 : n * Factorial(n -1);
}
/*
EXAMPLE:
1) concatenate the array elements, in order, to a string
*/
1)
var orderArray = function(x, indx, str) {
return indx == x.length - 1 ? str : orderArray(x, ++indx, (str += x[indx] + " "));
}
var arr = ['apple', 'orange', 'peach', 'lime'];
var str = orderArray(arr, -1, "");
// apple orange peach lime
console.log(str);
/*
EXAMPLE:
1) Tail Call Optimization
*/
1)
function factorial(num) {
if (num == 0) {
return 1;
} else { // Otherwise, call this recursive procedure again
return (num * factorial(num - 1));
}
}