Skip to content

Commit a9be4ce

Browse files
author
sunjieming
committed
Completed homework
1 parent 4a3f92c commit a9be4ce

File tree

14 files changed

+649
-24
lines changed

14 files changed

+649
-24
lines changed

.eslintrc.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"extends": "airbnb-base",
33
"plugins": [
44
"import"
5-
]
5+
],
6+
"consistent-return": 0,
7+
"rules": {
8+
"no-param-reassign": 0,
9+
"max-len": 0
10+
}
611
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#LambdaSchool
2+
##Advanced JavaScript

src/arrays.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
// pass by reference
1+
// Complete the following functions.
2+
// These functions only need to work with arrays.
3+
4+
/* eslint-disable no-unused-vars, max-len */
5+
6+
const each = (elements, cb) => {
7+
// Iterates over a list of elements, yielding each in turn to the `cb` function.
8+
// This only needs to work with arrays.
9+
// based off http://underscorejs.org/#each
10+
};
11+
12+
const map = (elements, cb) => {
13+
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
14+
// Return the new array.
15+
};
16+
17+
const reduce = (elements, cb, memo) => {
18+
// Combine all elements into a single value going from left to right.
19+
// Elements will be passed one by one into `cb`.
20+
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
21+
};
22+
23+
const find = (elements, cb) => {
24+
// Look through each value in `elements` and pass each element to `cb`.
25+
// If `cb` returns `true` then return that element.
26+
// Return `undefined` if no elements pass the truth test.
27+
};
28+
29+
const filter = (elements, cb) => {
30+
// Similar to `find` but you will return an array of all elements that passed the truth test
31+
// Return an empty array if no elements pass the truth test
32+
};
33+
34+
const flatten = (elements) => {
35+
// Flattens a nested array (the nesting can be to any depth).
36+
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
37+
};
38+
39+
/* eslint-enable no-unused-vars, max-len */
40+
41+
module.exports = {
42+
each,
43+
map,
44+
reduce,
45+
find,
46+
filter,
47+
flatten,
48+
};

src/callback-functions.js

Whitespace-only changes.

src/closure.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1+
// Complete the following functions.
12

2-
// counter
3-
const counter = () => {
4-
let num = 0;
5-
6-
3+
/* eslint-disable no-unused-vars */
74

5+
const counter = () => {
6+
// Return a function that when invoked increments and returns a counter variable.
7+
// Example: const newCounter = counter();
8+
// newCounter(); // 1
9+
// newCounter(); // 2
810
};
911

10-
// return an arrow function (this is the closure scope)
11-
12-
// private variables and functions
13-
14-
// cache
12+
const counterFactory = () => {
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.
16+
};
1517

16-
// loop
18+
const limitFunctionCallCount = (cb, n) => {
19+
// Should return a function that invokes `cb`.
20+
// The returned function should only allow `cb` to be invoked `n` times.
21+
};
1722

18-
// limit invocation count
23+
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.
30+
};
1931

20-
// counter factory
32+
/* eslint-enable no-unused-vars */
2133

2234
module.exports = {
2335
counter,
36+
counterFactory,
37+
cacheFunction,
38+
limitFunctionCallCount,
2439
};

src/es6.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
1-
// const
2-
// let
3-
// default parameters
4-
// arrow functions
5-
// ...
6-
// class
1+
/* eslint-disable */
2+
3+
// Refactor the following code to use the specified ES6 features.
4+
// There are no automated tests.
5+
// To make sure the code still works you can run this file using `node es6.js` from inside `/src`.
6+
7+
//----------------
8+
// const, =>, default parameters, arrow functions default return statements using ()
9+
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);
18+
19+
//----------------
20+
//const, =>, class, template literals, enhanced object literals (foo: foo, -> foo,)
21+
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+
};
28+
}
29+
30+
var username = 'JavaScriptForever';
31+
var password = 'password';
32+
33+
var me = new User({
34+
username: username,
35+
password: password,
36+
});
37+
38+
// ----------------
39+
// let, const, =>, ... (spread operator)
40+
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+
};
53+
54+
var result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15
55+
56+
/* eslint-enable */

src/objects.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
// pass by reference
1+
// Complete the following underscore functions.
2+
// Reference http://underscorejs.org/ for examples.
3+
4+
/* eslint-disable no-unused-vars */
5+
6+
const keys = (obj) => {
7+
// Retrieve all the names of the object's properties.
8+
// Return the keys as strings in an array.
9+
// Based on http://underscorejs.org/#keys
10+
};
11+
12+
const values = (obj) => {
13+
// Return all of the values of the object's own properties.
14+
// Ignore functions
15+
// http://underscorejs.org/#values
16+
};
17+
18+
const mapObject = (obj, cb) => {
19+
// Like map for arrays, but for objects. Transform the value of each property in turn.
20+
// http://underscorejs.org/#mapObject
21+
};
22+
23+
const pairs = (obj) => {
24+
// Convert an object into a list of [key, value] pairs.
25+
// http://underscorejs.org/#pairs
26+
};
27+
28+
const invert = (obj) => {
29+
// Returns a copy of the object where the keys have become the values and the values the keys.
30+
// Assume that all of the object's values will be unique and string serializable.
31+
// http://underscorejs.org/#invert
32+
};
33+
34+
const defaults = (obj, defaultProps) => {
35+
// Fill in undefined properties that match properties on the `defaultProps` parameter object.
36+
// Return `obj`.
37+
// http://underscorejs.org/#defaults
38+
};
39+
40+
/* eslint-enable no-unused-vars */
41+
42+
module.exports = {
43+
keys,
44+
values,
45+
mapObject,
46+
pairs,
47+
invert,
48+
defaults,
49+
};

src/prototype.js

Whitespace-only changes.

src/recursion.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Complete the following functions.
2+
3+
/* eslint-disable no-unused-vars */
4+
5+
const nFibonacci = (n) => {
6+
// fibonacci sequence: 1 2 3 5 8 13 ...
7+
// return the nth number in the sequence
8+
};
9+
10+
const nFactorial = (n) => {
11+
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
12+
// return the factorial of `n`
13+
};
14+
15+
const checkMatchingLeaves = (obj) => {
16+
// return true if every property on `obj` is the same
17+
// otherwise return false
18+
};
19+
20+
/* eslint-enable no-unused-vars */
21+
22+
module.exports = {
23+
nFibonacci,
24+
nFactorial,
25+
checkMatchingLeaves,
26+
};

src/this.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Follow the instructions and fill in the blank sections.
2+
// There are no tests for this file.
3+
// To verify your code works you can run this file using `node this.js` while in the `/src` folder
4+
5+
/* eslint-disable no-useless-constructor, no-unused-vars */
6+
7+
class User {
8+
constructor(options) {
9+
// set a username and password property on the user object that is created
10+
}
11+
// create a method on the User class called `checkPassword`
12+
// this method should take in a string and compare it to the object's password property
13+
// return `true` if they match, otherwise return `false`
14+
}
15+
16+
/* eslint-enable no-useless-constructor */
17+
18+
const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' });
19+
const result = me.checkPassword('correcthorsebatterystaple'); // should return `true`
20+
21+
const checkPassword = function comparePasswords(passwordToCompare) {
22+
// recreate the `checkPassword` method that you made on the `User` class
23+
// use `this` to access the object's `password` property.
24+
// do not modify this function's parameters
25+
// note that we use the `function` keyword and not `=>`
26+
};
27+
28+
/* eslint-enable no-unused-vars */
29+
// invoke `checkPassword` on `me` by explicitly setting the `this` context
30+
// use .call, .apply, and .bind
31+
32+
// .call
33+
34+
// .apply
35+
36+
// .bind

0 commit comments

Comments
 (0)