forked from lindakwoo/Udemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion.js
201 lines (164 loc) · 4.59 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
let power = (num,exp) =>{
if(exp===0){
return 1;
}
else{
return num*power(num,exp-1);
}
}
// console.log(power(5,3))
let factorial = (num)=>{
if(num===0){
return 1;
}else {
return num*factorial(num-1);
}
}
// console.log(factorial(6));
let productOfArray = (arr) =>{
if(arr.length===1){
return arr[0];
}
return arr[0]*productOfArray(arr.slice(1));
}
// console.log(productOfArray([3,3,3,2]))
let recursiveRange = (num)=>{
if(num===1){
return 1;
}
return num + recursiveRange(num-1);
}
// console.log(recursiveRange(9))
let fib = (num)=>{
if(num<=2){
return 1;
}
return fib(num-1) +fib(num-2);
}
// console.log(fib(6));
// with helper functions
let reverse = (str)=>{
let final = "";
let helpReverse=(helpStr)=>{
if(helpStr.length===0){
return;
}final+=helpStr[helpStr.length-1];
helpReverse(helpStr.substring(0,helpStr.length-1));
}
helpReverse(str);
return final;
}
// console.log(reverse("Linda Woo"))
let isPalindrome = (str)=>{
if(str.length === 0|| str.length ===1){
return true;
}
if(str[0]!==str[str.length-1]){
return false;
}
return isPalindrome(str.substring(1,str.length-1));
}
// console.log(isPalindrome("aadgdbaa"));
let someRecursive = (arr,func)=>{
if(arr.length===0){
return false;
}
if(func(arr[0])){
return true;
}
return someRecursive(arr.slice(1),func);
}
let greatherThanTen = num=> num>10;
// console.log(someRecursive([2,90,7],greatherThanTen));
let flatten = (arr)=>{
let final = [];
let flattenHelper = (arrHelper)=>{
if(arrHelper.length===0){
return;
}
if(!Array.isArray(arrHelper[0])){
final.push(arrHelper[0]);
} else{
flattenHelper(arrHelper[0]);
}
flattenHelper(arrHelper.slice(1));
}
flattenHelper(arr);
return final;
}
// console.log(flatten([[3,4],[1],[5,6],2]));
let capitalizeFirst = (arr) =>{
let final = [];
let helper = (helperArr) =>{
if(helperArr.length===0){
return;
}
final.push(helperArr[0][0].toUpperCase()+helperArr[0].slice(1));
return helper(helperArr.slice(1));
}
helper(arr);
return final;
}
// console.log(capitalizeFirst(["linda", "woo", "is", "capitalized!"]))
let nestedEvenSum = (obj) =>{
let sum = 0;
let helper = (helperObj)=>{
for (var key in helperObj) {
if (typeof helperObj[key] === 'object'){
sum += nestedEvenSum(helperObj[key]);
} else if (typeof helperObj[key] === 'number' && obj[key] % 2 === 0){
sum += helperObj[key];
}
}
}
helper(obj);
return sum;
}
// console.log(nestedEvenSum({dog:4,horse:3,cat:2}));
let capitalizeWords = (arr) =>{
let final = [];
let helper = (helperArr) =>{
if(helperArr.length===0){
return;
}
final.push(helperArr[0].toUpperCase());
return helper(helperArr.slice(1));
}
helper(arr);
return final;
}
// console.log(capitalizeWords(["linda","woo", "is", "now", "capitalized"]))
let stringifyNumbers = (obj)=>{
let finalObj = {};
for(let key in obj){
if(typeof obj[key]==="object"&&!Array.isArray(obj[key])){
finalObj[key]=stringifyNumbers(obj[key]);
}
else if(typeof obj[key]==="number"){
finalObj[key]=obj[key].toString();
}
else{
finalObj[key]=obj[key];
}
}
return finalObj
}
// console.log(stringifyNumbers({help:5,done:{number:4,title:"woo"}, anotherNumber:999,anArray:[], anotherWord: "linda"}));
let collectStrings = (obj)=>{
let final = [];
let helper = (helperObj)=>{
for(let key in helperObj){
console.log(helperObj[key]);
if(typeof helperObj[key]==="object"){
helper(helperObj[key]);
}
else if(typeof helperObj[key]==="string"){
console.log("it's a string");
final.push(helperObj[key]);
}
}
}
helper(obj);
return final;
}
// console.log(collectStrings({coward: "burg",data:{one:"mary", two:2, three:"three"},moreData:90,anotherObj:{more:"more",aFew:4}}))