-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.js
84 lines (71 loc) · 1.9 KB
/
recursion.js
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
// Use Recursion to Create a Countdown
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
console.log(countArray, n);
countArray.push(n);
// console.log(countArray);
return countArray;
}
}
// console.log(countup(5));
function countdown(n) {
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1);
console.log("recursive", n);
countArray.unshift(n);
return countArray;
}
}
// console.log(countdown(5));
// Use Recursion to Create a Range of Numbers
/**
*
* @param {*} startNum
* @param {*} endNum
* @returns an array of integers which begin with startNum and ends with endNum
* The code should also work where startNum and endNum are equal.
*/
function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum) {
return [];
} else {
const countArray = rangeOfNumbers(startNum + 1, endNum);
countArray.unshift(startNum);
return countArray;
}
};
// console.log(rangeOfNumbers(1, 1));
// Reverse a string
// testString = "12345"
testString = "Hello"
function reverseString(element) {
// if (element < 0) {
// return [];
// } else {
// const charArray = reverseString(element - 1);
// charArray.unshift(testString[element]);
// return charArray;
// }
if (element < 0) {
return "";
} else {
let newString = reverseString(element - 1);
newString = testString[element];
return newString
}
}
// reverse cleanString
// console.log(testString.length - 1);
// console.log(reverseString(testString.length - 1));
// newStringArray = reverseString(testString.length - 1);
// newString = "";
// for (const char of newStringArray) {
// newString += char
// }
newString = reverseString(testString.length - 1);
console.log(newString);