Skip to content

Commit 10207f0

Browse files
committed
JP Emery: Javascript-I completed tests, Objects.js is correct
1 parent 122abb0 commit 10207f0

File tree

4 files changed

+91
-17
lines changed

4 files changed

+91
-17
lines changed

src/arrays.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,27 @@ const each = (elements, cb) => {
1515
};
1616

1717
const map = (elements, cb) => {
18-
function multiplyByTwo(item) {
19-
return item * 2;
20-
}
21-
const newArr = elements.map(multiplyByTwo);
22-
return cb(newArr);
18+
const newArr = [];
2319

20+
for (let i = 0; i < elements.length; i++) {
21+
newArr.push(cb(elements[i]));
22+
}
23+
return newArr;
2424
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
2525
// Return the new array.
2626
};
2727

2828
const reduce = (elements, cb, startingValue) => {
29-
for (let i = 0; i < elements.length; i++) {
30-
const callbackItem = cb(startingValue, elements[i]);
29+
let startingIndex = 0;
30+
if (startingValue === undefined) {
31+
startingValue = elements[0];
32+
startingIndex = 1;
33+
}
34+
for (let i = startingIndex; i < elements.length; i++) {
35+
startingValue = cb(startingValue, elements[i]);
3136
}
37+
38+
return startingValue;
3239
// Combine all elements into a single value going from left to right.
3340
// Elements will be passed one by one into `cb` along with the `startingValue`.
3441
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
@@ -37,23 +44,40 @@ const reduce = (elements, cb, startingValue) => {
3744

3845
const find = (elements, cb) => {
3946
for (let i = 0; i < elements.length; i++) {
40-
const returning = cb(elements[i]);
41-
return returning;
47+
if (cb(elements[i] === true)) {
48+
return elements[i];
49+
}
4250
}
51+
return undefined;
4352
// Look through each value in `elements` and pass each element to `cb`.
4453
// If `cb` returns `true` then return that element.
4554
// Return `undefined` if no elements pass the truth test.
4655
};
4756

4857
const filter = (elements, cb) => {
49-
58+
const newArr = [];
59+
for (let i = 0; i < elements.length; i++) {
60+
if (cb(elements[i])) {
61+
newArr.push(elements[i]);
62+
}
63+
}
64+
return newArr;
5065
// Similar to `find` but you will return an array of all elements that passed the truth test
5166
// Return an empty array if no elements pass the truth test
5267
};
5368

5469
/* STRETCH PROBLEM */
5570

5671
const flatten = (elements) => {
72+
const newArr = [];
73+
for (let i = 0; i < elements.length; i++) {
74+
if (Array.isArray(elements[i])) {
75+
newArr.concat(flatten(elements[i]));
76+
} else {
77+
newArr.concat(elements[i]);
78+
}
79+
}
80+
return newArr;
5781
// Flattens a nested array (the nesting can be to any depth).
5882
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
5983
};

src/callbacks.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ const contains = (item, list, cb) => {
5959
/* STRETCH PROBLEM */
6060

6161
const removeDuplicates = (array, cb) => {
62-
// removeDuplicates removes all duplicate values from the given array.
62+
63+
let newArr = [];
64+
for (let i = 0 ; i < array.length; i++) {
65+
let index = newArr.indexOf(array[i]);
66+
if(index === -1) {
67+
newArr.push(array[i]);
68+
}
69+
}
70+
return cb(newArr);
71+
// removeDuplicates removes all duplicate values from the given array.
6372
// Pass the duplicate free array to the callback function.
6473
// Do not mutate the original array.
6574
};

src/closure.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,62 @@
11
// Complete the following functions.
22

33
const counter = () => {
4+
let count = 1;
5+
function increments() {
6+
return count++;
7+
}
8+
return increments;
49
// Return a function that when invoked increments and returns a counter variable.
510
// Example: const newCounter = counter();
611
// newCounter(); // 1
712
// newCounter(); // 2
813
};
914

1015
const counterFactory = () => {
16+
let count = 0;
17+
18+
return {
19+
increment: () => {
20+
return ++count;
21+
},
22+
decrement: () => {
23+
return --count;
24+
},
25+
};
1126
// Return an object that has two methods called `increment` and `decrement`.
1227
// `increment` should increment a counter variable in closure scope and return it.
1328
// `decrement` should decrement the counter variable and return it.
1429
};
1530

1631
const limitFunctionCallCount = (cb, n) => {
32+
let count = n;
33+
34+
return () => {
35+
if (count > 0) {
36+
--count;
37+
return cb();
38+
}
39+
return null;
40+
};
1741
// Should return a function that invokes `cb`.
1842
// The returned function should only allow `cb` to be invoked `n` times.
1943
};
2044

2145
/* STRETCH PROBLEM */
2246

2347
const cacheFunction = (cb) => {
48+
const cache = {};
49+
return (...args) => {
50+
const keys = Object.keys(cache);
51+
const argsString = args.toString();
52+
if (keys.indexOf(argsString) !== -1) {
53+
return cache[argsString];
54+
}
55+
const result = cb(...args);
56+
cache[argsString] = result;
57+
return result;
58+
};
59+
2460
// Should return a funciton that invokes `cb`.
2561
// A cache (object) should be kept in closure scope.
2662
// The cache should keep track of all arguments have been used to invoke this function.

src/objects.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22
// Reference http://underscorejs.org/ for examples.
33

44
const keys = (obj) => {
5-
for (let i = 0; i < Object.keys(obj).length; i++) {
6-
const value = [];
7-
value.push(Object.keys(obj[i]));
8-
return value;
9-
}
10-
return value;
5+
return Object.keys(obj);
116
// Retrieve all the names of the object's properties.
127
// Return the keys as strings in an array.
138
// Based on http://underscorejs.org/#keys
149
};
1510

1611
const values = (obj) => {
12+
return Object.values(obj);
1713
// Return all of the values of the object's own properties.
1814
// Ignore functions
1915
// http://underscorejs.org/#values
2016
};
2117

2218
const mapObject = (obj, cb) => {
19+
const newObj = {};
20+
const key = Object.keys(obj);
21+
for (let i = 0; i < key.length; i++) {
22+
const newVal = cb(obj[key[i]]);
23+
newObj[key[i]] = newVal;
24+
}
25+
return newObj;
2326
// Like map for arrays, but for objects. Transform the value of each property in turn.
2427
// http://underscorejs.org/#mapObject
2528
};
2629

2730
const pairs = (obj) => {
31+
return Object.entries(obj);
2832
// Convert an object into a list of [key, value] pairs.
2933
// http://underscorejs.org/#pairs
3034
};
@@ -38,6 +42,7 @@ const invert = (obj) => {
3842
};
3943

4044
const defaults = (obj, defaultProps) => {
45+
Object.assign(defaultProps, obj);
4146
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
4247
// Return `obj`.
4348
// http://underscorejs.org/#defaults

0 commit comments

Comments
 (0)