-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (170 loc) · 3.75 KB
/
index.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
class ArrayProc {
constructor(arr) {
this.arr = arr;
}
at(index) {
let indexValue;
for (let indx = 0; indx < this.arr.length; indx++) {
let valueAtEachIndex = this.arr[index];
if (indx == index) {
indexValue = valueAtEachIndex;
break;
}
}
return indexValue;
}
concat(arr) {
let concatArray = [];
for (let index = 0; index < this.arr.length; index++) {
concatArray.push(this.arr[index]);
}
for (let index = 0; index < arr.length; index++) {
concatArray.push(arr[index]);
}
return concatArray;
}
copywithin(start, from, end = this.arr.length) {
for (let index = from; index < end; index++) {
this.arr[start] = this.arr[index];
start++;
}
return this.arr;
}
every(func) {
for (let index = 0; index < this.arr.length; index++) {
const element = this.arr[index];
if (!func(element)) {
return false;
}
}
return true;
}
fill(value, start, end = this.arr.length) {
const arra = this.arr;
for (let index = start; index < end; index++) {
arra[index] = value;
}
return arra;
}
filter(func) {
let filtered = [];
for (let index = 0; index < this.arr.length; index++) {
if (func(this.arr[index])) {
filtered.push(this.arr[index]);
}
}
return filtered;
}
find(func) {
let filtered = [];
for (let index = 0; index < this.arr.length; index++) {
if (func(this.arr[index])) {
filtered.push(this.arr[index]);
break;
}
}
return filtered.length === 0 ? undefined : filtered;
}
findIndex(func) {
let ind;
for (let index = 0; index < this.arr.length; index++) {
if (func(this.arr[index])) {
ind = index;
break;
}
}
return ind === undefined ? -1 : ind;
}
findIndexR(func) {
let ind;
for (let index = this.arr.length - 1; index >= 0; index--) {
if (func(this.arr[index])) {
ind = index;
break;
}
}
return ind === undefined ? -1 : ind;
}
findIndexB(func) {
let ind;
for (let index = 0; index < this.arr.length; index++) {
if (func(this.arr[index])) {
ind = index;
}
}
return ind === undefined ? -1 : ind;
}
}
function a(n) {
// base case
if (n == 0) {
return 1;
}
// recursive case
return n * a(n - 1);
}
function callMeAgain(n) {
// base case
if (n == 0) {
return;
}
// recursive case
console.log(n, ": call me again");
callMeAgain(n - 1);
}
callMeAgain(10);
function flat(arr, depth) {
// base case;
let f = [];
let level = 1;
function flattener(arr, level) {
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (Array.isArray(element)) {
if (depth == level) {
f.push(element);
break;
}
level++;
f.concat(flattener(element, level++));
} else {
f.push(element);
}
}
return f;
}
return flattener(arr, level);
}
function flat2(arr) {
// base case;
let f = [];
function flattener(arr) {
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (Array.isArray(element)) {
f.concat(flattener(element));
} else {
f.push(element);
}
}
return f;
}
return flattener(arr);
}
function flatMap(arr, func) {
// base case;
let f = [];
function flattener(arr) {
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (Array.isArray(element)) {
f.concat(flattener(element));
} else {
f.push(func(element));
}
}
return f;
}
return flattener(arr);
}
console.log(flatMap([1, [2, 3, 5]], (x) => x ** 2));