Skip to content

Commit 45b8eea

Browse files
committed
Homework1
1 parent c268e18 commit 45b8eea

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/arrays.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,58 @@ 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.push(cb(elements[i]));
23+
} return newArr;
1724
};
1825

1926
const reduce = (elements, cb, startingValue) => {
2027
// Combine all elements into a single value going from left to right.
2128
// Elements will be passed one by one into `cb` along with the `startingValue`.
2229
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
2330
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
31+
const comboV = elements.slice();
32+
let memo;
33+
if (startingValue === undefined) {
34+
memo = comboV.shift();
35+
} else {
36+
memo = startingValue;
37+
}
38+
each(comboV, (item) => {
39+
memo = cb(memo, item);
40+
});
41+
return memo;
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]) === true) {
50+
return elements[i];
51+
}
52+
}
3053
};
3154

3255
const filter = (elements, cb) => {
3356
// Similar to `find` but you will return an array of all elements that passed the truth test
3457
// Return an empty array if no elements pass the truth test
58+
const newArr = [];
59+
for (let i = 0; i <= elements.length; i++) {
60+
if (cb(elements[i]) === true) {
61+
newArr.push(elements[i]);
62+
}
63+
} return newArr;
3564
};
3665

3766
/* STRETCH PROBLEM */

src/callbacks.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,54 @@
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.
26-
};
31+
for( let i=0;i<list.length;i++){
32+
if(list[i]===item){
33+
cb(true)}
34+
if(list[i]!==item){
35+
cb(false)
36+
}
37+
}
38+
};
2739

2840
/* STRETCH PROBLEM */
2941

3042
const removeDuplicates = (array, cb) => {
3143
// removeDuplicates removes all duplicate values from the given array.
3244
// Pass the duplicate free array to the callback function.
3345
// Do not mutate the original array.
46+
let newArr=[array[0]];
47+
for(let i=1;i<length;i++){
48+
if(array[i]!==array[i-1] && (!newArr.includes(array[i]))) {
49+
newArr.push(array[i]);
50+
}
51+
}
52+
return cb(newArr);
3453
};
3554

3655
/* eslint-enable */

src/objects.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1+
12
// Complete the following underscore functions.
23
// Reference http://underscorejs.org/ for examples.
34

45
const keys = (obj) => {
56
// Retrieve all the names of the object's properties.
67
// Return the keys as strings in an array.
78
// Based on http://underscorejs.org/#keys
9+
return Object.keys(obj);
810
};
911

1012
const values = (obj) => {
1113
// Return all of the values of the object's own properties.
1214
// Ignore functions
1315
// http://underscorejs.org/#values
16+
return Object.values(obj);
1417
};
1518

1619
const mapObject = (obj, cb) => {
1720
// Like map for arrays, but for objects. Transform the value of each property in turn.
1821
// http://underscorejs.org/#mapObject
22+
const myKeys = Object.keys(obj);
23+
for (let i = 0; i < myKeys.length; i++) {
24+
obj[myKeys[i]] = cb(obj[myKeys]);
25+
}
26+
return obj;
1927
};
2028

2129
const pairs = (obj) => {
2230
// Convert an object into a list of [key, value] pairs.
2331
// http://underscorejs.org/#pairs
32+
return Object.entries(obj);
2433
};
2534

2635
/* STRETCH PROBLEMS */

0 commit comments

Comments
 (0)