Skip to content

Commit bc3db5e

Browse files
committed
Matthew Tomas Javascript I WIP
1 parent a121a83 commit bc3db5e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/arrays.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ const each = (elements, cb) => {
1414
// This only needs to work with arrays.
1515
// You should also pass the index into `cb` as the second argument
1616
// based off http://underscorejs.org/#each
17+
for (let i = 0; i < elements.length; i++) {
18+
cb(elements[i]);
19+
}
1720
};
1821

1922
const map = (elements, cb) => {
2023
// Do NOT use .map, to complete this function.
2124
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
2225
// Return the new array.
26+
2327
};
2428

2529
const reduce = (elements, cb, startingValue) => {
@@ -28,6 +32,12 @@ const reduce = (elements, cb, startingValue) => {
2832
// Elements will be passed one by one into `cb` along with the `startingValue`.
2933
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
3034
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
35+
let memo = elements.shift();
36+
if (startingValue !== undefined) memo = cb(memo, startingValue);
37+
for (let i = 0; i < elements.length; i++) {
38+
memo = cb(memo, elements[i]);
39+
}
40+
return memo;
3141
};
3242

3343
const find = (elements, cb) => {
@@ -48,6 +58,15 @@ const filter = (elements, cb) => {
4858
const flatten = (elements) => {
4959
// Flattens a nested array (the nesting can be to any depth).
5060
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
61+
let newArr = [];
62+
for (let i = 0; i < elements.length; i++) {
63+
if (Array.isArray(elements[i])) {
64+
newArr = newArr.concat(flatten(elements[i]));
65+
} else {
66+
newArr.push(elements[i]);
67+
}
68+
}
69+
return newArr;
5170
};
5271

5372
/* eslint-enable no-unused-vars, max-len */

src/callbacks.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
const firstItem = (arr, cb) => {
22
// firstItem passes the first item of the given array to the callback function.
3+
cb(arr[0]);
34
};
45

56
const getLength = (arr, cb) => {
67
// getLength passes the length of the array into the callback.
8+
cb(arr.length);
79
};
810

911
const last = (arr, cb) => {
1012
// last passes the last item of the array into the callback.
13+
cb(arr[arr.length - 1]);
1114
};
1215

1316
const sumNums = (x, y, cb) => {
1417
// sumNums adds two numbers (x, y) and passes the result to the callback.
18+
cb(x + y);
1519
};
1620

1721
const multiplyNums = (x, y, cb) => {
1822
// multiplyNums multiplies two numbers and passes the result to the callback.
23+
cb(x * y);
1924
};
2025

2126
const contains = (item, list, cb) => {

0 commit comments

Comments
 (0)