-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursionPractice.js
More file actions
156 lines (139 loc) · 2.65 KB
/
recursionPractice.js
File metadata and controls
156 lines (139 loc) · 2.65 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
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
function loop(base,expo){
let count = 0;
let result = [];
while ( count < expo) {
count++
result.push(base)
}
return result.reduce((a,b) => {
return a * b
})
}
function recurse(base,expo){
if (expo === 1) {
return base;
}
return base * recurse(base,expo-1)
}
function reverseArrloop(arr){
return arr.reverse()
}
function reverseArr(arr){
let result = [];
function help(){
if (arr.length > 0){
result.push(arr.pop())
help()
}
}
help()
return result;
}
function reverseString(str){
return str.split('').reverse().join('')
}
function reverseString(str){
let result = "";
let count = str.length
function help(){
if (str.length > 0){
count--
result += str[str.length-1]
str = str.slice(0,count)
help()
}
}
help()
return result
}
reverseString('justin')
//[1,2,3],3 === [3,6,9]
function recursiveMultiplier(arr,num){
let result = [];
function help(){
if (arr.length > 0){
result.push(arr.shift() * num)
help()
}
}
help()
return result
}
recursiveMultiplier([1,2,3],4)
function multiply(arr){
var copy = arr.slice()
var result =[];
function help(){
if(arr.length > 0){
result.push(arr.shift() * 2)
help();
}
}
help();
return "" + result === "" + copy.map((element) => {
return element * 2
})
}
function find(arr,k){
var count = 0;
function help(){
if (arr[count] !== k){
count++
help();
}
}
help();
return count;
}
find([10,9,8,7,6,5,4,3,2,1],7)
function range(x,y){
var count = x;
var result = [];
function help(){
if(x < y-1){
count++
x++
result.push(count)
help();
}
}
help();
return result
}
range(2, 9)
function count(str){
var obj = {};
var count = 0;
function help(){
if (count < str.length){
if(str[count] in obj){
obj[str[count]]++
count++
} else {
obj[str[count]] = 1;
count++
}
help();
}
}
help();
return obj;
}
count('thisisgoingtowork')
function find(obj,val){
var arr = [];
function findBob(obj){
for (var key in obj){
if (obj[key] === val){
return true
} else{
if (typeof obj[key] === 'object'){
return findBob(obj[key])
}
}
}
}
findBob(obj)
return findBob(obj)
}
find(obj,"cool")