forked from rithmschool/intermediate_javascript_exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.js
More file actions
181 lines (145 loc) · 3.97 KB
/
recursion.js
File metadata and controls
181 lines (145 loc) · 3.97 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// - Write a function called `productOfArray` which takes in an array of numbers and returns the product of them all
function productOfArray(arr) {
if (arr.length === 1) {
return arr[0];
}
return arr[0] * productOfArray(arr.slice(1))
}
// - Write a function called `collectStrings` which accepts an object and returns an array of all the values in the object that have a typeof string
function collectStrings(obj) {
var arr = []
function helper(a){
for (var key in a){
if (typeof(a[key]) === "object") {
helper(a[key])
} else {
arr.push(a[key])
}
}
}
helper(obj)
return arr;
}
// - Write a function called `contains` that searches for a value in a nested object. It returns true if the object contains that value.
function contains(nestedObject, value) {
for (var key in nestedObject) {
if (typeof(nestedObject[key]) === "object") {
if (contains(nestedObject[key], value)) {
return true
};
}
if (nestedObject[key] === value) {
return true;
}
}
return false
}
// - [https://www.codewars.com/kata/the-real-size-of-a-multi-dimensional-array/train/javascript](https://www.codewars.com/kata/the-real-size-of-a-multi-dimensional-array/train/javascript)
function realSize(arrays) {
var count = 0;
function helper(a) {
for (var i =0; i < a.length; i++) {
if (Array.isArray(a[i]) === true) {
helper(a[i])
}else if (Number.isInteger(a[i])) {
count++;
}
}
}
helper(arrays)
return count;
}
// - [https://www.codewars.com/kata/sum-squares-of-numbers-in-list-that-may-contain-more-lists/train/javascript](https://www.codewars.com/kata/sum-squares-of-numbers-in-list-that-may-contain-more-lists/train/javascript)
function SumSquares(l){
var sum = 0
function helper(a) {
for (var i = 0; i < a.length; i++) {
if (Array.isArray(a[i]) === true) {
helper(a[i])
} else if (Number.isInteger(a[i])) {
sum += (a[i] * a[i])
}
}
}
helper(l)
return sum
}
// - [https://www.codewars.com/kata/recursive-replication](https://www.codewars.com/kata/recursive-replication)
function replicate(times, number) {
if (times === 1) {
return [number];
}
if (times < 1) {
return [];
}
return [number].concat(replicate(times-1, number))
}
// Write a function called search that finds a value in an array and returns the index where the value is at. If the value is not found, the function should return negative 1.
function search(arr,val) {
var index = 0
function helper(a,v) {
for (var i = 0; i < a.length; i++) {
if (a[i] === v) {
return i
}
}
return -1;
}
return helper(arr,val)
}
//Refactor your search function to use a faster algorithm called binary search https://www.youtube.com/watch?v=JQhciTuD3E8.
function binarySearch(arr,val) {
var l = 0;
var r = arr.length - 1;
var middle = Math.floor((l+r)/2);
function helper(a,v,l,r) {
middle = Math.floor((l+r)/2);
if (l > r) {
return middle = -1;
}
if (a[middle] === v) {
return middle
} else if (v > a[middle]) {
l = middle+1
helper(a,v,l, r)
} else {
r = middle -1
helper(a,v, l, r)
}
}
helper(arr,val,l,r)
return middle
}
// //iterative
// function binary(arr, val) {
// var l = 0
// var r = arr.length-1
// var middle = 0;
// while (true) {
// if (l > r) {
// return -1
// }
// middle = Math.floor((l+r)/2)
// if (arr[middle] === val) {
// return middle
// } else if (val > arr[middle]) {
// l = middle +1
// } else {
// r = middle - 1
// }
// }
// }
//- Write a function called `stringifyNumbers` which takes in an object and finds all of the values which are numbers and converts them to strings. Recursion would be a great way to solve this!
function stringifyNumbers(obj) {
var answer = {}
for (var key in obj) {
if (typeof obj[key] === "number") {
answer[key] = obj[key].toString();
} else if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
answer[key] = stringifyNumbers(obj[key])
} else {
answer[key] = obj[key];
}
}
return answer;
}