Skip to content

Commit f724a29

Browse files
committed
finish arrays and callbacks
1 parent d95404a commit f724a29

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎src/arrays.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,63 @@ 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 newArray = [];
21+
for (let i = 0; i < elements.length; i++) {
22+
newArray.push(cb(elements[i]));
23+
}
24+
return newArray;
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 value;
33+
if (startingValue === undefined) {
34+
value = elements[0];
35+
for (let i = 1; i < elements.length; i++) {
36+
value = cb(value, elements[i]);
37+
}
38+
} else {
39+
value = startingValue;
40+
for (let i = 0; i < elements.length; i++) {
41+
value = cb(value, elements[i]);
42+
}
43+
}
44+
return value;
2445
};
2546

2647
const find = (elements, cb) => {
2748
// Look through each value in `elements` and pass each element to `cb`.
2849
// If `cb` returns `true` then return that element.
2950
// Return `undefined` if no elements pass the truth test.
51+
for (let i = 0; i < elements.length; i++) {
52+
if (cb(elements[i]) === true) {
53+
return elements[i];
54+
}
55+
}
56+
return undefined;
3057
};
3158

3259
const filter = (elements, cb) => {
3360
// Similar to `find` but you will return an array of all elements that passed the truth test
3461
// Return an empty array if no elements pass the truth test
62+
const truth = [];
63+
for (let i = 0; i < elements.length; i++) {
64+
if (cb(elements[i]) === true) {
65+
truth.push(elements[i]);
66+
}
67+
}
68+
return truth;
3569
};
3670

3771
/* STRETCH PROBLEM */

‎src/callbacks.js‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,54 @@ const firstItem = (arr, cb) => {
77

88
const getLength = (arr, cb) => {
99
// getLength passes the length of the array into the callback.
10-
cb(arr.length); // do i have ur code ryan ? ahh
10+
cb(arr.length);
1111
};
1212

1313
const last = (arr, cb) => {
1414
// last passes the last item of the array into the callback.
15+
cb(arr[arr.length - 1]);
1516
};
1617

1718
const sumNums = (x, y, cb) => {
1819
// sumNums adds two numbers (x, y) and passes the result to the callback.
20+
cb(x + y);
1921
};
2022

2123
const multiplyNums = (x, y, cb) => {
2224
// multiplyNums multiplies two numbers and passes the result to the callback.
25+
cb(x * y);
2326
};
2427

2528
const contains = (item, list, cb) => {
2629
// contains checks if an item is present inside of the given array/list.
2730
// Pass true to the callback if it is, otherwise pass false.
31+
var found = false;
32+
for (let i = 0; i < list.length; i++){
33+
if (list[i] === item)
34+
found = true;
35+
}
36+
cb(found);
2837
};
2938

3039
/* STRETCH PROBLEM */
31-
40+
function find(list, item){
41+
for (let i = 0; i < list.length; i++){
42+
if (list[i] === item)
43+
return true;
44+
}
45+
return false;
46+
}
3247
const removeDuplicates = (array, cb) => {
3348
// removeDuplicates removes all duplicate values from the given array.
3449
// Pass the duplicate free array to the callback function.
3550
// Do not mutate the original array.
51+
var dups = [];
52+
for (let i = 0; i < array.length; i++){
53+
if (!find(dups, array[i])) {
54+
dups.push(array[i]);
55+
}
56+
}
57+
cb(dups);
3658
};
3759

3860
/* eslint-enable */

0 commit comments

Comments
 (0)