Skip to content

Commit e485014

Browse files
committed
Intial commit
1 parent dc8c373 commit e485014

4 files changed

Lines changed: 114 additions & 4 deletions

File tree

src/arrays.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,81 @@
55
// You can use the functions that you have already written to help solve the other problems
66

77
const each = (elements, cb) => {
8+
for (let i = 0; i < elements.length; i++) {
9+
cb(elements[i], i);
10+
}
811
// Iterates over a list of elements, yielding each in turn to the `cb` function.
912
// This only needs to work with arrays.
1013
// You should also pass the index into `cb` as the second argument
1114
// based off http://underscorejs.org/#each
1215
};
1316

1417
const map = (elements, cb) => {
18+
const mappedArr = [];
19+
for (let i = 0; i < elements.length; i++) {
20+
const ele = cb(elements[i], i);
21+
mappedArr.push(ele);
22+
}
23+
return mappedArr;
1524
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
1625
// Return the new array.
1726
};
1827

1928
const reduce = (elements, cb, startingValue) => {
29+
let memo = startingValue;
30+
let start = 0;
31+
if (memo === undefined) {
32+
memo = elements[0];
33+
start = 1;
34+
}
35+
for (let i = start; i < elements.length; i++) {
36+
memo = cb(memo, elements[i]);
37+
}
38+
return memo;
2039
// Combine all elements into a single value going from left to right.
2140
// Elements will be passed one by one into `cb` along with the `startingValue`.
2241
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
2342
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
2443
};
2544

2645
const find = (elements, cb) => {
46+
for (let i = 0; i < elements.length; i++) {
47+
if (cb(elements[i])) {
48+
return elements[i];
49+
}
50+
}
51+
return undefined;
2752
// Look through each value in `elements` and pass each element to `cb`.
2853
// If `cb` returns `true` then return that element.
2954
// Return `undefined` if no elements pass the truth test.
3055
};
3156

3257
const filter = (elements, cb) => {
58+
const result = [];
59+
for (let i = 0; i < elements.length; i++) {
60+
const ele = elements[i];
61+
if (cb(ele)) {
62+
result.push(ele);
63+
}
64+
}
65+
return result;
3366
// Similar to `find` but you will return an array of all elements that passed the truth test
3467
// Return an empty array if no elements pass the truth test
3568
};
3669

3770
/* STRETCH PROBLEM */
3871

3972
const flatten = (elements) => {
73+
let flatArr = [];
74+
for (let i = 0; i < elements.length; i++) {
75+
const ele = elements[i];
76+
if (Array.isArray(ele)) {
77+
flatArr = flatArr.concat(flatten(ele));
78+
} else {
79+
flatArr.push(ele);
80+
}
81+
}
82+
return flatArr;
4083
// Flattens a nested array (the nesting can be to any depth).
4184
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
4285
};

src/callbacks.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
/* eslint-disable */
22

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

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

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

1518
const sumNums = (x, y, cb) => {
19+
const sum = x + y;
20+
cb(sum);
1621
// sumNums adds two numbers (x, y) and passes the result to the callback.
1722
};
1823

1924
const multiplyNums = (x, y, cb) => {
25+
const product = x * y;
26+
cb(product);
2027
// multiplyNums multiplies two numbers and passes the result to the callback.
2128
};
2229

2330
const contains = (item, list, cb) => {
31+
return cb(list.includes(item));
2432
// contains checks if an item is present inside of the given array/list.
2533
// Pass true to the callback if it is, otherwise pass false.
2634
};
2735

2836
/* STRETCH PROBLEM */
2937

3038
const removeDuplicates = (array, cb) => {
39+
const newArr = [];
40+
for (let i = 0; i < array.length; i++) {
41+
const ele = array[i];
42+
if(newArr.indexOf(ele) < 0) {
43+
newArr.push(ele)}
44+
};
45+
return cb(newArr)
3146
// removeDuplicates removes all duplicate values from the given array.
3247
// Pass the duplicate free array to the callback function.
3348
// Do not mutate the original array.

src/closure.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
11
// Complete the following functions.
22

33
const counter = () => {
4+
let count = 0;
5+
return function () {
6+
return ++count;
7+
};
48
// Return a function that when invoked increments and returns a counter variable.
59
// Example: const newCounter = counter();
610
// newCounter(); // 1
711
// newCounter(); // 2
812
};
13+
// Return an object that has two methods called `increment` and `decrement`.
14+
// `increment` should increment a counter variable in closure scope and return it.
15+
// `decrement` should decrement the counter variable and return it.
916

1017
const counterFactory = () => {
11-
// Return an object that has two methods called `increment` and `decrement`.
12-
// `increment` should increment a counter variable in closure scope and return it.
13-
// `decrement` should decrement the counter variable and return it.
18+
let count = 0;
19+
return {
20+
increment: () => {
21+
return ++count;
22+
},
23+
decrement: () => {
24+
return --count;
25+
},
26+
};
1427
};
1528

1629
const limitFunctionCallCount = (cb, n) => {
30+
let count = 0;
31+
return (...args) => {
32+
if (count < n) {
33+
count++;
34+
return cb(...args);
35+
}
36+
return null;
37+
};
1738
// Should return a function that invokes `cb`.
1839
// The returned function should only allow `cb` to be invoked `n` times.
1940
};
2041

2142
/* STRETCH PROBLEM */
2243

2344
const cacheFunction = (cb) => {
24-
// Should return a funciton that invokes `cb`.
45+
const cache = {};
46+
return (args) => {
47+
if (!(args in cache)) {
48+
cache[args] = cb(args);
49+
}
50+
return cache[args];
51+
};
52+
// Should return a funciton that invokes `cb`. check
2553
// A cache (object) should be kept in closure scope.
2654
// The cache should keep track of all arguments have been used to invoke this function.
2755
// If the returned function is invoked with arguments that it has already seen

src/objects.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,60 @@
22
// Reference http://underscorejs.org/ for examples.
33

44
const keys = (obj) => {
5+
const props = Object.keys(obj);
6+
return props;
57
// Retrieve all the names of the object's properties.
68
// Return the keys as strings in an array.
79
// Based on http://underscorejs.org/#keys
810
};
911

1012
const values = (obj) => {
13+
const valOfProp = Object.values(obj);
14+
return valOfProp;
1115
// Return all of the values of the object's own properties.
1216
// Ignore functions
1317
// http://underscorejs.org/#values
1418
};
1519

1620
const mapObject = (obj, cb) => {
21+
Object.keys(obj).forEach(key => (obj[key] = cb(obj[key])));
22+
return obj;
1723
// Like map for arrays, but for objects. Transform the value of each property in turn.
1824
// http://underscorejs.org/#mapObject
1925
};
2026

2127
const pairs = (obj) => {
28+
const result = [];
29+
// Object.keys(obj).forEach(key => (result.push[key, obj[key]]));
30+
Object.keys(obj).forEach((key) => {
31+
const pair = [key, obj[key]];
32+
result.push(pair);
33+
});
34+
return result;
2235
// Convert an object into a list of [key, value] pairs.
2336
// http://underscorejs.org/#pairs
2437
};
2538

2639
/* STRETCH PROBLEMS */
2740

2841
const invert = (obj) => {
42+
const newObj = {};
43+
Object.keys(obj).forEach((key) => {
44+
newObj[obj[key]] = key;
45+
});
46+
return newObj;
2947
// Returns a copy of the object where the keys have become the values and the values the keys.
3048
// Assume that all of the object's values will be unique and string serializable.
3149
// http://underscorejs.org/#invert
3250
};
3351

3452
const defaults = (obj, defaultProps) => {
53+
Object.keys(defaultProps).forEach((key) => {
54+
if (obj[key] === undefined) {
55+
obj[key] = defaultProps[key];
56+
}
57+
});
58+
return obj;
3559
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
3660
// Return `obj`.
3761
// http://underscorejs.org/#defaults

0 commit comments

Comments
 (0)