Skip to content

Commit 246d66c

Browse files
authored
Add files via upload
array.js homework complete
1 parent a552c45 commit 246d66c

File tree

1 file changed

+87
-50
lines changed

1 file changed

+87
-50
lines changed

src/arrays.js

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,87 @@
1-
// Complete the following functions.
2-
// These functions only need to work with arrays.
3-
// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc.
4-
// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating
5-
// You can use the functions that you have already written to help solve the other problems
6-
7-
const each = (elements, cb) => {
8-
// Iterates over a list of elements, yielding each in turn to the `cb` function.
9-
// This only needs to work with arrays.
10-
// based off http://underscorejs.org/#each
11-
};
12-
13-
const map = (elements, cb) => {
14-
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
15-
// Return the new array.
16-
};
17-
18-
const reduce = (elements, cb, memo = elements.shift()) => {
19-
// Combine all elements into a single value going from left to right.
20-
// Elements will be passed one by one into `cb`.
21-
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
22-
};
23-
24-
const find = (elements, cb) => {
25-
// Look through each value in `elements` and pass each element to `cb`.
26-
// If `cb` returns `true` then return that element.
27-
// Return `undefined` if no elements pass the truth test.
28-
};
29-
30-
const filter = (elements, cb) => {
31-
// Similar to `find` but you will return an array of all elements that passed the truth test
32-
// Return an empty array if no elements pass the truth test
33-
};
34-
35-
/* Extra Credit */
36-
const flatten = (elements) => {
37-
// Flattens a nested array (the nesting can be to any depth).
38-
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
39-
};
40-
41-
/* eslint-enable no-unused-vars, max-len */
42-
43-
module.exports = {
44-
each,
45-
map,
46-
reduce,
47-
find,
48-
filter,
49-
flatten
50-
};
1+
// Complete the following functions.
2+
// These functions only need to work with arrays.
3+
// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc.
4+
// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating
5+
// You can use the functions that you have already written to help solve the other problems
6+
7+
const each = (elements, cb) => {
8+
// Iterates over a list of elements, yielding each in turn to the `cb` function.
9+
// This only needs to work with arrays.
10+
// based off http://underscorejs.org/#each
11+
for (let i = 0; i < elements.length; i++) {
12+
cb(elements[i], i);
13+
}
14+
};
15+
16+
const map = (elements, cb) => {
17+
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
18+
// Return the new array.
19+
const newArray = [];
20+
for (let i = 0; i < elements.length; i++) {
21+
newArray.push(cb(elements[i]));
22+
}
23+
return newArray;
24+
25+
};
26+
27+
const reduce = (elements, cb, memo = elements.shift()) => {
28+
// Combine all elements into a single value going from left to right.
29+
// Elements will be passed one by one into `cb`.
30+
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
31+
let reduced = memo;
32+
for (let i = 0; i < elements.length; i++) {
33+
result = cb(reduced, elements[i]);
34+
}
35+
return reduced;
36+
};
37+
38+
const find = (elements, cb) => {
39+
// Look through each value in `elements` and pass each element to `cb`.
40+
// If `cb` returns `true` then return that element.
41+
// Return `undefined` if no elements pass the truth test.
42+
let truthy;
43+
for (let i = 0; i < elements.length; i++) {
44+
if (cb(elements[i])) {
45+
truthy = elements[i];
46+
}
47+
}
48+
return truthy;
49+
};
50+
51+
const filter = (elements, cb) => {
52+
// Similar to `find` but you will return an array of all elements that passed the truth test
53+
// Return an empty array if no elements pass the truth test
54+
const truthyArr = [];
55+
for (let i = 0; i < elements.length; i++) {
56+
if (cb(elements[i])) {
57+
truthyArr.push(elements[i]);
58+
}
59+
}
60+
return truthyArr;
61+
};
62+
63+
/* Extra Credit */
64+
const flatten = (elements) => {
65+
// Flattens a nested array (the nesting can be to any depth).
66+
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
67+
const result = [];
68+
for(let i = 0; i < elements.length; i++) {
69+
if(Array.isArray(elements[i])) {
70+
result = result.concat(flatten(elements[i]));
71+
} else {
72+
result.push(elements[i]);
73+
}
74+
}
75+
return result;
76+
};
77+
78+
/* eslint-enable no-unused-vars, max-len */
79+
80+
module.exports = {
81+
each,
82+
map,
83+
reduce,
84+
find,
85+
filter,
86+
flatten
87+
};

0 commit comments

Comments
 (0)