Skip to content

Commit 85d3dc6

Browse files
committed
callbacks completed
1 parent dc8c373 commit 85d3dc6

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎src/arrays.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,56 @@ const each = (elements, cb) => {
99
// This only needs to work with arrays.
1010
// You should also pass the index into `cb` as the second argument
1111
// based off http://underscorejs.org/#each
12+
for (let i = 0; i < elements.length; i++) {
13+
cb(elements[i], i);
14+
}
1215
};
1316

1417
const map = (elements, cb) => {
1518
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
1619
// Return the new array.
20+
const newArr = [];
21+
for (let i = 0; i < elements.length; i++) {
22+
newArr[i] = cb(elements[i]);
23+
}
24+
return newArr;
1725
};
1826

1927
const reduce = (elements, cb, startingValue) => {
2028
// Combine all elements into a single value going from left to right.
2129
// Elements will be passed one by one into `cb` along with the `startingValue`.
2230
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
2331
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
32+
let i = 0;
33+
if (startingValue === undefined) {
34+
startingValue = elements[0];
35+
i = 1;
36+
}
37+
let combined = startingValue;
38+
for (; i < elements.length; i++) {
39+
combined = cb(combined, elements[i]);
40+
}
41+
return combined;
2442
};
2543

2644
const find = (elements, cb) => {
2745
// Look through each value in `elements` and pass each element to `cb`.
2846
// If `cb` returns `true` then return that element.
2947
// Return `undefined` if no elements pass the truth test.
48+
for (let i = 0; i < elements.length; i++) {
49+
if (cb(elements[i])) return elements[i];
50+
}
51+
return undefined;
3052
};
3153

3254
const filter = (elements, cb) => {
3355
// Similar to `find` but you will return an array of all elements that passed the truth test
3456
// Return an empty array if no elements pass the truth test
57+
const newArr = [];
58+
for (let i = 0; i < elements.length; i++) {
59+
if (cb(elements[i])) newArr.push(elements[i]);
60+
}
61+
return newArr;
3562
};
3663

3764
/* STRETCH PROBLEM */

‎src/callbacks.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@
22

33
const firstItem = (arr, cb) => {
44
// firstItem passes the first item of the given array to the callback function.
5+
cb(arr[0]);
56
};
67

78
const getLength = (arr, cb) => {
89
// getLength passes the length of the array into the callback.
10+
cb(arr.length);
911
};
1012

1113
const last = (arr, cb) => {
1214
// last passes the last item of the array into the callback.
15+
cb(arr[arr.length-1]);
1316
};
1417

1518
const sumNums = (x, y, cb) => {
1619
// sumNums adds two numbers (x, y) and passes the result to the callback.
20+
cb(x+y);
1721
};
1822

1923
const multiplyNums = (x, y, cb) => {
2024
// multiplyNums multiplies two numbers and passes the result to the callback.
25+
cb(x * y);
2126
};
2227

2328
const contains = (item, list, cb) => {
2429
// contains checks if an item is present inside of the given array/list.
2530
// Pass true to the callback if it is, otherwise pass false.
31+
cb(list.includes(item));
2632
};
2733

2834
/* STRETCH PROBLEM */
@@ -31,6 +37,12 @@ const removeDuplicates = (array, cb) => {
3137
// removeDuplicates removes all duplicate values from the given array.
3238
// Pass the duplicate free array to the callback function.
3339
// Do not mutate the original array.
40+
const fltrdArr = [];
41+
for (let i = 0; i < array.length; i++) {
42+
if (fltrdArr.indexOf(array[i]) === -1 && array[i] !== '')
43+
fltrdArr.push(array[i]);
44+
}
45+
cb(fltrdArr);
3446
};
3547

3648
/* eslint-enable */

‎src/objects.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ const keys = (obj) => {
55
// Retrieve all the names of the object's properties.
66
// Return the keys as strings in an array.
77
// Based on http://underscorejs.org/#keys
8+
return Object.keys(obj);
89
};
910

1011
const values = (obj) => {
1112
// Return all of the values of the object's own properties.
1213
// Ignore functions
1314
// http://underscorejs.org/#values
15+
return keys(obj).map((item) => {
16+
return obj[item];
17+
});
1418
};
1519

1620
const mapObject = (obj, cb) => {
1721
// Like map for arrays, but for objects. Transform the value of each property in turn.
1822
// http://underscorejs.org/#mapObject
23+
// return Object.keys(obj).forEach((el) => {obj[el] = cb(obj[el]) return obj;});
1924
};
2025

2126
const pairs = (obj) => {

0 commit comments

Comments
 (0)