Skip to content

Commit fe0a4d2

Browse files
committed
completed assignment
1 parent 31b3b6e commit fe0a4d2

7 files changed

Lines changed: 156 additions & 45 deletions

File tree

‎package.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "LambdaSchool's JavaScript Mini-Bootcamp Homework 1",
55
"main": "index.js",
66
"scripts": {
7-
"test": "./node_modules/.bin/eslint tests/*.js && ./node_modules/.bin/eslint src/*.js && jest",
7+
"test": "eslint tests/*.js && eslint src/*.js && jest",
88
"test:watch": "npm test -- --watch"
99
},
1010
"repository": {
@@ -27,6 +27,8 @@
2727
},
2828
"dependencies": {
2929
"babel-preset-es2015": "^6.24.0",
30-
"eslint-config-airbnb": "^14.1.0"
30+
"eslint-config-airbnb": "^14.1.0",
31+
"eslint-plugin-jsx-a11y": "^4.0.0",
32+
"eslint-plugin-react": "^6.10.3"
3133
}
3234
}

‎src/arrays.js‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,74 @@
33

44
/* eslint-disable no-unused-vars, max-len */
55

6+
// const each = (elements, cb) => elements.forEach((el, index) => cb(el, index));
67
const each = (elements, cb) => {
78
// Iterates over a list of elements, yielding each in turn to the `cb` function.
89
// This only needs to work with arrays.
910
// based off http://underscorejs.org/#each
11+
for (let i = 0; i < elements.length; i++) {
12+
cb(elements[i], i);
13+
}
1014
};
1115

16+
// const map = (elements, cb) => elements.map(el => cb(el));
1217
const map = (elements, cb) => {
1318
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
1419
// Return the new array.
20+
const arr = [];
21+
for (let i = 0; i < elements.length; i++) {
22+
arr.push(cb(elements[i], i));
23+
}
24+
return arr;
1525
};
1626

17-
const reduce = (elements, cb, memo) => {
27+
// const reduce = (elements, cb, memo = 0) => elements.reduce((tot, el) => cb(tot, el), memo);
28+
const reduce = (elements, cb, memo = 0) => {
1829
// Combine all elements into a single value going from left to right.
1930
// Elements will be passed one by one into `cb`.
2031
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
32+
// const result = arrayMethods.reduce(arr, (memo, num) => (memo + num), 10);
33+
let sum = memo;
34+
35+
for (let i = 0; i < elements.length; i++) {
36+
sum = cb(sum, elements[i]);
37+
}
38+
return sum;
2139
};
2240

41+
// const find = (elements, cb) => elements.find(el => cb(el));
2342
const find = (elements, cb) => {
2443
// Look through each value in `elements` and pass each element to `cb`.
2544
// If `cb` returns `true` then return that element.
2645
// Return `undefined` if no elements pass the truth test.
46+
let val;
47+
48+
for (let i = 0; i < elements.length; i++) {
49+
if (cb(elements[i])) {
50+
val = elements[i];
51+
break;
52+
}
53+
val = undefined;
54+
}
55+
return val;
2756
};
2857

58+
// const filter = (elements, cb) => elements.filter(el => cb(el));
2959
const filter = (elements, cb) => {
3060
// Similar to `find` but you will return an array of all elements that passed the truth test
3161
// Return an empty array if no elements pass the truth test
62+
const passed = [];
63+
for (let i = 0; i < elements.length; i++) {
64+
if (cb(elements[i])) passed.push(elements[i]);
65+
}
66+
return passed;
3267
};
3368

34-
const flatten = (elements) => {
69+
const flatten = elements => elements.reduce(
70+
(arr, el) => arr.concat(Array.isArray(el) ? flatten(el) : el), []);
3571
// Flattens a nested array (the nesting can be to any depth).
3672
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
37-
};
73+
3874

3975
/* eslint-enable no-unused-vars, max-len */
4076

‎src/closure.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,42 @@ const counter = () => {
77
// Example: const newCounter = counter();
88
// newCounter(); // 1
99
// newCounter(); // 2
10+
let count = 0;
11+
12+
return () => {
13+
count += 1;
14+
return count;
15+
};
1016
};
1117

1218
const counterFactory = () => {
1319
// Return an object that has two methods called `increment` and `decrement`.
1420
// `increment` should increment a counter variable in closure scope and return it.
1521
// `decrement` should decrement the counter variable and return it.
22+
let count = 0;
23+
24+
return {
25+
increment: () => {
26+
count += 1;
27+
return count;
28+
},
29+
decrement: () => {
30+
count -= 1;
31+
return count;
32+
},
33+
};
1634
};
1735

1836
const limitFunctionCallCount = (cb, n) => {
1937
// Should return a function that invokes `cb`.
2038
// The returned function should only allow `cb` to be invoked `n` times.
39+
let count = 0;
40+
41+
return (...args) => {
42+
count += 1;
43+
if (count <= n) return cb(...args);
44+
return null;
45+
};
2146
};
2247

2348
const cacheFunction = (cb) => {
@@ -27,6 +52,12 @@ const cacheFunction = (cb) => {
2752
// If the returned function is invoked with arguments that it has already seen
2853
// then it should return the cached result and not invoke `cb` again.
2954
// `cb` should only ever be invoked once for a given set of arguments.
55+
const cache = {};
56+
57+
return (x) => {
58+
if (!hasOwnProperty.call(cache, x)) cache[x] = cb(x);
59+
return cache[x];
60+
};
3061
};
3162

3263
/* eslint-enable no-unused-vars */

‎src/es6.js‎

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,33 @@
77
//----------------
88
// const, =>, default parameters, arrow functions default return statements using ()
99

10-
var food = 'pineapple';
11-
12-
var isMyFavoriteFood = function(food) {
13-
food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey
14-
return food === 'thousand-year-old egg';
15-
};
16-
17-
var isThisMyFavorite = isMyFavoriteFood(food);
10+
const food = 'pineapple';
11+
const isMyFavoriteFood = (food = 'thousand-year-old egg') => food === 'thousand-year-old egg';
12+
const isThisMyFavorite = isMyFavoriteFood(food);
1813

1914
//----------------
2015
//const, class, template literals, enhanced object literals (foo: foo, -> foo,)
2116

22-
var User = function(options) {
23-
this.username = options.username;
24-
this.password = options.password;
25-
this.sayHi = function() {
26-
return this.username + ' says hello!';
27-
};
17+
class User {
18+
constructor(options) {
19+
this.username = options.username;
20+
this.password = options.password;
21+
}
22+
sayHi() {
23+
return `${this.username} says hello!`;
24+
}
2825
}
2926

30-
var username = 'JavaScriptForever';
31-
var password = 'password';
32-
33-
var me = new User({
34-
username: username,
35-
password: password,
36-
});
27+
const username = 'JavaScriptForever';
28+
const password = 'password';
29+
const me = new User({ username, password });
3730

3831
// ----------------
3932
// let, const, =>, ... (spread operator)
4033

41-
var addArgs = function () {
42-
var sum = 0;
43-
for (var i = 0; i < arguments.length; i++) {
44-
sum += arguments[i];
45-
}
46-
return sum;
47-
};
48-
49-
var argsToCb = function (cb) {
50-
var args = Array.prototype.slice.call(arguments);
51-
return cb.apply(null, args.splice(1));
52-
};
34+
const addArgs = (...args) => args.reduce((sum, num) => sum + num);
35+
const argsToCb = (cb, ...args) => cb.apply(null, args);
5336

54-
var result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15
37+
const result = argsToCb(addArgs, 1, 2, 3, 4, 5); // result should be 15
5538

5639
/* eslint-enable */

‎src/objects.js‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,60 @@
33

44
/* eslint-disable no-unused-vars */
55

6-
const keys = (obj) => {
6+
const keys = obj => Object.keys(obj);
77
// Retrieve all the names of the object's properties.
88
// Return the keys as strings in an array.
99
// Based on http://underscorejs.org/#keys
10-
};
1110

12-
const values = (obj) => {
11+
const values = obj => Object.keys(obj).map(key => obj[key]);
1312
// Return all of the values of the object's own properties.
1413
// Ignore functions
1514
// http://underscorejs.org/#values
16-
};
1715

16+
// const mapObject = (obj, cb) => Object.keys(obj).map(key => { cb(obj[key]) });
1817
const mapObject = (obj, cb) => {
1918
// Like map for arrays, but for objects. Transform the value of each property in turn.
2019
// http://underscorejs.org/#mapObject
20+
const copy = {};
21+
const items = keys(obj);
22+
23+
for (let i = 0, len = items.length; i < len; i++) {
24+
copy[items[i]] = cb(obj[items[i]]);
25+
}
26+
return copy;
2127
};
2228

23-
const pairs = (obj) => {
29+
const pairs = obj => Object.keys(obj).map(key => [key, obj[key]]);
2430
// Convert an object into a list of [key, value] pairs.
2531
// http://underscorejs.org/#pairs
26-
};
2732

2833
const invert = (obj) => {
2934
// Returns a copy of the object where the keys have become the values and the values the keys.
3035
// Assume that all of the object's values will be unique and string serializable.
3136
// http://underscorejs.org/#invert
37+
const copy = {};
38+
const arr = pairs(obj);
39+
40+
for (let i = 0, len = arr.length; i < len; i++) {
41+
copy[arr[i][1]] = arr[i][0];
42+
}
43+
return copy;
3244
};
3345

3446
const defaults = (obj, defaultProps) => {
3547
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
3648
// Return `obj`.
3749
// http://underscorejs.org/#defaults
50+
const objKeys = pairs(obj);
51+
const defKeys = pairs(defaultProps);
52+
53+
for (let i = 0, len = defKeys.length; i < len; i++) {
54+
const key = defKeys[i][0];
55+
const val = defKeys[i][1];
56+
57+
if (!obj[key]) obj[key] = val;
58+
}
59+
return obj;
3860
};
3961

4062
/* eslint-enable no-unused-vars */

‎src/recursion.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,44 @@
55
const nFibonacci = (n) => {
66
// fibonacci sequence: 1 2 3 5 8 13 ...
77
// return the nth number in the sequence
8+
if (n <= 1) return 1;
9+
return nFibonacci(n - 1) + nFibonacci(n - 2);
810
};
911

1012
const nFactorial = (n) => {
1113
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
1214
// return the factorial of `n`
15+
if (n === 0) return 1;
16+
return n * nFactorial(n - 1);
1317
};
1418

1519
const checkMatchingLeaves = (obj) => {
1620
// return true if every property on `obj` is the same
1721
// otherwise return false
22+
const values = [];
23+
24+
const flatten = (set) => {
25+
const keys = Object.keys(set);
26+
27+
for (let i = 0, len = keys.length; i < len; i++) {
28+
if (typeof set[keys[i]] !== 'object') {
29+
values.push(set[keys[i]]);
30+
} else {
31+
return flatten(set[keys[i]]);
32+
}
33+
}
34+
return null;
35+
};
36+
37+
const checkVals = (vals) => {
38+
for (let i = 0, len = vals.length; i < len; i++) {
39+
if (vals[i] !== vals[0]) return false;
40+
}
41+
return true;
42+
};
43+
44+
flatten(obj);
45+
return checkVals(values);
1846
};
1947

2048
/* eslint-enable no-unused-vars */

‎src/this.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
class User {
88
constructor(options) {
99
// set a username and password property on the user object that is created
10+
this.username = options.username;
11+
this.password = options.password;
1012
}
1113
// create a method on the User class called `checkPassword`
1214
// this method should take in a string and compare it to the object's password property
1315
// return `true` if they match, otherwise return `false`
16+
checkPassword(str) {
17+
return this.password === str;
18+
}
1419
}
1520

1621
/* eslint-enable no-useless-constructor */
@@ -23,14 +28,18 @@ const checkPassword = function comparePasswords(passwordToCompare) {
2328
// use `this` to access the object's `password` property.
2429
// do not modify this function's parameters
2530
// note that we use the `function` keyword and not `=>`
31+
return this.password === passwordToCompare;
2632
};
2733

2834
/* eslint-enable no-unused-vars */
2935
// invoke `checkPassword` on `me` by explicitly setting the `this` context
3036
// use .call, .apply, and .bind
3137

3238
// .call
39+
checkPassword.call(me, 'correcthorsebatterystaple');
3340

3441
// .apply
42+
checkPassword.apply(me, ['correcthorsebatterystaple']);
3543

3644
// .bind
45+
checkPassword.bind(me)('correcthorsebatterystaple');

0 commit comments

Comments
 (0)