Skip to content

Commit 579f98f

Browse files
Troy WilliamsTroy Williams
authored andcommitted
completed
1 parent d95404a commit 579f98f

4 files changed

Lines changed: 158 additions & 12 deletions

File tree

src/arrays.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,101 @@ 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+
each(elements, (item, index) => {
22+
newArr.push(cb(item, index));
23+
});
24+
return newArr;
1725
};
1826

1927
const reduce = (elements, cb, startingValue) => {
28+
// arr, (memo, num) => (memo + num)
2029
// Combine all elements into a single value going from left to right.
2130
// Elements will be passed one by one into `cb` along with the `startingValue`.
2231
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
2332
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
33+
// let reduced;
34+
// if (startingValue === undefined) {
35+
// reduced = elements[0];
36+
// for (let i = 1; i < elements.length; i++) {
37+
// reduced = cb(reduced, elements[i]);
38+
// }
39+
// } else {
40+
// reduced = startingValue;
41+
// for (let i = 0; i < elements.length; i++) {
42+
// reduced = cb(reduced, elements[i]);
43+
// }
44+
// }
45+
// return reduced;
46+
const newElements = elements.slice();
47+
if (startingValue === undefined) {
48+
startingValue = newElements.shift();
49+
}
50+
let memo = startingValue;
51+
each(newElements, (el) => {
52+
memo = cb(memo, el);
53+
});
54+
return memo;
2455
};
2556

57+
2658
const find = (elements, cb) => {
2759
// Look through each value in `elements` and pass each element to `cb`.
2860
// If `cb` returns `true` then return that element.
2961
// Return `undefined` if no elements pass the truth test.
62+
let result;
63+
for (let i = 0; i < elements.length; i++) {
64+
if (cb(elements[i]) === true) {
65+
result = elements[i];
66+
} else {
67+
result = undefined;
68+
}
69+
}
70+
return result;
3071
};
3172

3273
const filter = (elements, cb) => {
3374
// Similar to `find` but you will return an array of all elements that passed the truth test
3475
// Return an empty array if no elements pass the truth test
76+
const newArr = [];
77+
for (let i = 0; i < elements.length; i++) {
78+
if (cb(elements[i]) === true) {
79+
newArr.push(elements[i]);
80+
}
81+
}
82+
return newArr;
3583
};
3684

3785
/* STRETCH PROBLEM */
3886

3987
const flatten = (elements) => {
4088
// Flattens a nested array (the nesting can be to any depth).
4189
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
90+
let outPut = [];
91+
each(elements, (num) => {
92+
if (Array.isArray(num)) {
93+
outPut = outPut.concat(flatten(num));
94+
} else {
95+
outPut.push(num);
96+
}
97+
});
98+
// for (let i = 0; i < elements.length; i++) {
99+
// for (let j = 0; j < elements[i].length; i++) {
100+
// newArr.push(elements[i][j]);
101+
// }
102+
// }
103+
return outPut;
42104
};
43-
105+
// const myArr = [1, [2], [3, [[4]]]];
106+
// console.log(flatten(myArr));
44107
/* eslint-enable no-unused-vars, max-len */
45108

46109
module.exports = {

src/callbacks.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,53 @@ 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+
let sum = x + y;
21+
cb(sum);
1922
};
2023

2124
const multiplyNums = (x, y, cb) => {
2225
// multiplyNums multiplies two numbers and passes the result to the callback.
26+
let prod = x * y;
27+
cb(prod);
2328
};
2429

2530
const contains = (item, list, cb) => {
2631
// contains checks if an item is present inside of the given array/list.
2732
// Pass true to the callback if it is, otherwise pass false.
33+
for (let i = 0; i <list.length; i++){
34+
if (list[i] === item){
35+
cb(true)
36+
} else {
37+
cb(false)
38+
}
39+
};
2840
};
29-
3041
/* STRETCH PROBLEM */
3142

3243
const removeDuplicates = (array, cb) => {
3344
// removeDuplicates removes all duplicate values from the given array.
3445
// Pass the duplicate free array to the callback function.
3546
// Do not mutate the original array.
47+
// [2,3,5,2,5,7]
48+
let obj = {};
49+
let resultArr = [];
50+
for (let i = 0; i < array.length; i++){
51+
if (!(array[i] in obj)) {
52+
resultArr.push(array[i])
53+
obj[array[i]] = true;
54+
}
55+
}
56+
cb(resultArr);
3657
};
3758

3859
/* eslint-enable */

src/closure.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,51 @@ const counter = () => {
55
// Example: const newCounter = counter();
66
// newCounter(); // 1
77
// newCounter(); // 2
8+
let count = 0;
9+
return () => {
10+
count++;
11+
return count;
12+
};
813
};
914

1015
const counterFactory = () => {
1116
// Return an object that has two methods called `increment` and `decrement`.
1217
// `increment` should increment a counter variable in closure scope and return it.
1318
// `decrement` should decrement the counter variable and return it.
19+
let count = 0;
20+
return {
21+
increment() {
22+
return ++count;
23+
},
24+
decrement() {
25+
return --count;
26+
},
27+
};
1428
};
15-
1629
const limitFunctionCallCount = (cb, n) => {
1730
// Should return a function that invokes `cb`.
1831
// The returned function should only allow `cb` to be invoked `n` times.
32+
let count = 0;
33+
return (...args) => {
34+
if (count >= n) {
35+
return null;
36+
}++count;
37+
return cb(...args);
38+
};
1939
};
20-
2140
/* STRETCH PROBLEM */
2241

2342
const cacheFunction = (cb) => {
24-
// Should return a funciton that invokes `cb`.
25-
// A cache (object) should be kept in closure scope.
26-
// The cache should keep track of all arguments have been used to invoke this function.
27-
// If the returned function is invoked with arguments that it has already seen
28-
// then it should return the cached result and not invoke `cb` again.
29-
// `cb` should only ever be invoked once for a given set of arguments.
43+
const cacheObj = {};
44+
return (...args) => {
45+
const key = args;
46+
if (!(key in cacheObj)) {
47+
const newVal = cb(...args);
48+
cacheObj[key] = newVal;
49+
return newVal;
50+
}
51+
return cacheObj.key;
52+
};
3053
};
3154

3255
/* eslint-enable no-unused-vars */
@@ -37,3 +60,12 @@ module.exports = {
3760
cacheFunction,
3861
limitFunctionCallCount,
3962
};
63+
64+
// const hi = 'hello';
65+
// const say = () => {
66+
// console.log(hi);
67+
// };
68+
// const dosome = () => {
69+
// say();
70+
// };
71+
// dosome();

src/objects.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,38 @@ 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 Object.values(obj);
1416
};
1517

1618
const mapObject = (obj, cb) => {
1719
// Like map for arrays, but for objects. Transform the value of each property in turn.
1820
// http://underscorejs.org/#mapObject
21+
const result = {};
22+
const objKeys = Object.keys(obj);
23+
const objValues = Object.values(obj);
24+
for (let i = 0; i < objValues.length; i++) {
25+
result[objKeys[i]] = cb(objValues[i]);
26+
}
27+
return result;
1928
};
2029

2130
const pairs = (obj) => {
2231
// Convert an object into a list of [key, value] pairs.
2332
// http://underscorejs.org/#pairs
33+
const pairsArr = [];
34+
const objkeys = Object.keys(obj);
35+
const objValues = Object.values(obj);
36+
for (let i = 0; i < objkeys.length; i++) {
37+
pairsArr.push([objkeys[i], objValues[i]]);
38+
}
39+
return pairsArr;
2440
};
2541

2642
/* STRETCH PROBLEMS */
@@ -29,14 +45,28 @@ const invert = (obj) => {
2945
// Returns a copy of the object where the keys have become the values and the values the keys.
3046
// Assume that all of the object's values will be unique and string serializable.
3147
// http://underscorejs.org/#invert
48+
const result = {};
49+
const objKeys = Object.keys(obj);
50+
const objValues = Object.values(obj);
51+
for (let i = 0; i < objKeys.length; i++) {
52+
result[objValues[i]] = objKeys[i];
53+
}
54+
return result;
3255
};
3356

3457
const defaults = (obj, defaultProps) => {
3558
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
3659
// Return `obj`.
3760
// http://underscorejs.org/#defaults
61+
const defaultKeys = Object.keys(defaultProps);
62+
const defaultValues = Object.values(defaultProps);
63+
for (let i = 0; i < defaultKeys.length; i++) {
64+
if (!(defaultKeys[i] in obj)) {
65+
obj[defaultKeys[i]] = defaultValues[i];
66+
}
67+
}
68+
return obj;
3869
};
39-
4070
/* eslint-enable no-unused-vars */
4171

4272
module.exports = {

0 commit comments

Comments
 (0)