-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy patharrays.js
More file actions
130 lines (118 loc) · 4.42 KB
/
arrays.js
File metadata and controls
130 lines (118 loc) · 4.42 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
/*
Complete the following functions.
These functions only need to work with arrays.
A few of these functions mimic the behavior of the `Built` in JavaScript Array Methods.
The idea here is to recreate the functions from scratch BUT if you'd like,
feel free to Re-use any of your functions you build within your other functions.
You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating
You can use the functions that you have already written to help solve the other problems
*/
const each = (elements, cb) => {
// Do NOT use forEach to complete this function.
// Iterates over a list of elements, yielding each in turn to the `cb` function.
// This only needs to work with arrays.
// You should also pass the index into `cb` as the second argument
// based off http://underscorejs.org/#each
for (let i = 0; i < elements.length; i++) {
// pass each el and its index in the given array to a cb
cb(elements[i], i);
}
};
const map = (elements, cb) => {
// Do NOT use .map, to complete this function.
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
// Return the new array.
// hold transformed els into an array
const newArr = [];
for (let i = 0; i < elements.length; i++) {
// transform each el in the given array and push the result to newArr
newArr.push(cb(elements[i]));
}
// output: array containing the transformed els
return newArr;
};
const reduce = (elements, cb, startingValue) => {
// Do NOT use .reduce, to complete this function.
// Combine all elements into a single value going from left to right.
// Elements will be passed one by one into `cb` along with the `startingValue`.
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
// a holds either the starting value or the first el
let a;
if (startingValue) {
a = startingValue;
} else {
// if startingValue is falsy assign a to the first el of the given arr
a = elements.shift();
}
// copy the given arr
const sliced = elements.slice();
for (let i = 0; i < sliced.length; i++) {
// pass a and els of sliced into a cb
// set a to the result of the combining function
a = cb(a, sliced[i]);
}
// output: result of combining all the els in the given arr with cb
return a;
};
const find = (elements, cb) => {
// Do NOT use .includes, to complete this function.
// Look through each value in `elements` and pass each element to `cb`.
// If `cb` returns `true` then return that element.
// Return `undefined` if no elements pass the truth test.
for (let i = 0; i < elements.length; i++) {
// iterate over the given arr
if (cb(elements[i])) {
// only return elements for which cb return true
return elements[i];
}
}
};
const filter = (elements, cb) => {
// Do NOT use .filter, to complete this function.
// Similar to `find` but you will return an array of all elements that passed the truth test
// Return an empty array if no elements pass the truth test
// hold the els that pass the check in a new array
const result = [];
for (let i = 0; i < elements.length; i++) {
// check if the els pass the test in cb
if (cb(elements[i])) {
result.push(elements[i]);
}
}
// output: array that holds all elements that passed a test in cb
return result;
};
/* STRETCH PROBLEM */
const flatten = (elements) => {
// Flattens a nested array (the nesting can be to any depth).
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
const flat = [];
function flattenArr(a) {
// base case: el in arr is not an array
if (!Array.isArray(a)) {
flat.push(a);
} else {
// recursive case: el is an array
// iterate over a: go one level into el
a.forEach(el => flattenArr(el));
}
return flat;
}
return flattenArr(elements);
};
// test cases courtesy of FreeCodeCamp
// https://www.freecodecamp.org/challenges/steamroller
// flatten([1, [2], [3, [[4]]]]) returns [1, 2, 3, 4]
// flatten([1, [], [3, [[4]]]]) returns [1, 3, 4]
// flatten([[["a"]], [["b"]]]) returns ["a", "b"]
// flatten([1, {}, [3, [[4]]]]) returns [1, {}, 3, 4]
/* eslint-enable no-unused-vars, max-len */
module.exports = {
each,
map,
reduce,
find,
filter,
flatten
};