From 9793db40973147a40866c0fc52ae6de06f6648e7 Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Thu, 7 Sep 2017 03:39:26 -0400 Subject: [PATCH 1/8] Project-Arrays W1(B) Array Section Completed. Extra Credit : In-Progress. --- src/arrays.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index b995952..50f616f 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -8,34 +8,55 @@ const each = (elements, cb) => { // Iterates over a list of elements, yielding each in turn to the `cb` function. // This only needs to work with arrays. // based off http://underscorejs.org/#each + for (let i = 0; i < elements.length; i++) { cb(elements[i], i); } }; const map = (elements, cb) => { // Produces a new array of values by mapping each value in list through a transformation function (iteratee). // Return the new array. + const newArr = []; + for (let i = 0; i < elements.length; i++) { newArr.push(cb(elements[i])); } + return newArr; }; const reduce = (elements, cb, memo = elements.shift()) => { // Combine all elements into a single value going from left to right. // Elements will be passed one by one into `cb`. - // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value. + // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value + if (memo === undefined) memo = elements[0]; + for (let i = 0; i < elements.length; ++i) { + memo = cb(memo, elements[i]); + } + return memo; }; + const find = (elements, cb) => { // Look through each value in `elements` and pass each element to `cb`. // If `cb` returns `true` then return that element. // Return `undefined` if no elements pass the truth test. + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) return elements[i]; + } + return undefined; }; const filter = (elements, cb) => { // Similar to `find` but you will return an array of all elements that passed the truth test // Return an empty array if no elements pass the truth test + const newArr = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) newArr.push(elements[i]); + } + return newArr; }; /* Extra Credit */ const flatten = (elements) => { // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; + const newArr = [].concat(...elements); + return newArr; }; /* eslint-enable no-unused-vars, max-len */ From 6e966da0ce7ee1f557f181cbd25ab1a0c9d45dbb Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Thu, 7 Sep 2017 04:05:27 -0400 Subject: [PATCH 2/8] Project W1(B) Array Section & Extra Credit Completed. --- src/arrays.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index 50f616f..41f65f4 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -55,7 +55,14 @@ const filter = (elements, cb) => { const flatten = (elements) => { // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; - const newArr = [].concat(...elements); + const newArr = []; + for (let i = 0; i < elements.length; i++) { + if (elements[i].length) { + elements = elements.concat(elements[i]); + } else { + newArr.push(elements[i]); + } + } return newArr; }; From 8a574b6bdd3f1ff30a093aeb36c4fb047e88a6c9 Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Fri, 8 Sep 2017 10:06:36 -0400 Subject: [PATCH 3/8] Project W1(B) -Arrays: Completed -Class: Completed --- src/class.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/class.js b/src/class.js index 8276e29..aaee2d7 100644 --- a/src/class.js +++ b/src/class.js @@ -19,6 +19,31 @@ // `meow` that should return the string ` meowed!` where `` is the `name` // property set on the Cat instance. +class User { + constructor(options) { + this.email = options.email; + this.password = options.password; + } + comparePasswords(pass) { + if (pass === this.password) return true; + return false; + } +} +class Animal { + constructor(options) { + this.age = options.age; + } + growOlder() { return ++this.age; } + +} +class Cat extends Animal { + constructor(options) { + super(options); + this.name = super.name; + } + meow() { return `${this.name} meowed!`; } +} + module.exports = { User, From bf0eddcb5924f3ea2e4a6a73e9172973f04d57b0 Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Fri, 8 Sep 2017 19:40:52 -0400 Subject: [PATCH 4/8] Project W1(B) -Arrays: Completed -Class: Completed -Closure: Completed --- src/closure.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/closure.js b/src/closure.js index 2d6592f..f57925c 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,17 +5,46 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + const inc = () => { + count++; + return count; + }; + return inc; +}; +// +const newObj = { + numUp: 0, + numDown: 0, + increment: function increment() { + this.numUp++; + return this.numUp; + }, + decrement: function decrement() { + this.numDown--; + return this.numDown; + } }; - const counterFactory = () => { // Return an object that has two methods called `increment` and `decrement`. // `increment` should increment a counter variable in closure scope and return it. // `decrement` should decrement the counter variable and return it. + return newObj; }; + const limitFunctionCallCount = (cb, n) => { // Should return a function that invokes `cb`. - // The returned function should only allow `cb` to be invoked `n` times. + // The returned function should only allow `cb` to be invoked `n` times + let count = 0; + return (...args) => { + if (count < n) { + count++; + } else { + return null; + } + return cb(...args); + }; }; /* Extra Credit */ @@ -26,6 +55,11 @@ const cacheFunction = (cb) => { // If the returned function is invoked with arguments that it has already seen // then it should return the cached result and not invoke `cb` again. // `cb` should only ever be invoked once for a given set of arguments. + const cache = {}; + return (arg) => { + if (arg in cache) return cache[arg]; + return cache[arg] = cb(arg); + }; }; /* eslint-enable no-unused-vars */ From 8227a574df2c178b2e001b9dedd13be3578068ea Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Fri, 8 Sep 2017 22:13:47 -0400 Subject: [PATCH 5/8] Project W1(B) -Arrays: Completed -Class: Completed -Closure: Completed -ES6: Completed --- src/es6.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/es6.js b/src/es6.js index eb846ab..baa93ff 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,30 +7,30 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +let food = 'pineapple'; -var isMyFavoriteFood = function(food) { +const isMyFavoriteFood = (food) => { food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey return food === 'thousand-year-old egg'; }; -var isThisMyFavorite = isMyFavoriteFood(food); +const isThisMyFavorite = isMyFavoriteFood(food); //---------------- //const, class, template literals, enhanced object literals (foo: foo, -> foo,) -var User = function(options) { +const User = (options) => { this.username = options.username; this.password = options.password; - this.sayHi = function() { + this.sayHi = () => { return this.username + ' says hello!'; }; } -var username = 'JavaScriptForever'; -var password = 'password'; +const username = 'JavaScriptForever'; +const password = 'password'; -var me = new User({ +const me = User({ username: username, password: password, }); @@ -38,19 +38,19 @@ var me = new User({ // ---------------- // let, const, =>, ... (spread operator) -var addArgs = function () { - var sum = 0; - for (var i = 0; i < arguments.length; i++) { - sum += arguments[i]; +const addArgs = (...arg) => { + let sum = 0; + for (let i = 0; i < arg.length; i++) { + sum += arg[i]; } return sum; }; -var argsToCb = function (cb) { - var args = Array.prototype.slice.call(arguments); +const argsToCb = (cb, ...arg) => { + const args = Array.prototype.slice.call(arg); return cb.apply(null, args.splice(1)); }; -var result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 +const result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 /* eslint-enable */ From 40506cf533d7b04259ef5f2fca776de4f9516b4b Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Sat, 9 Sep 2017 02:59:25 -0400 Subject: [PATCH 6/8] Project W1(B) -Arrays: Completed -Class: Completed -Closure: Completed -ES6: Completed -Objects: Completed --- package-lock.json | 5 +++++ package.json | 3 ++- src/objects.js | 29 ++++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index c2d7130..baf523a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3945,6 +3945,11 @@ "dev": true, "optional": true }, + "underscore": { + "version": "1.8.3", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" + }, "user-home": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/user-home/-/user-home-2.0.0.tgz", diff --git a/package.json b/package.json index d23afbd..cecedb5 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "dependencies": { "babel-preset-es2015": "^6.24.0", - "eslint-config-airbnb": "^14.1.0" + "eslint-config-airbnb": "^14.1.0", + "underscore": "^1.8.3" } } diff --git a/src/objects.js b/src/objects.js index ba39c6c..2699b31 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,38 +1,57 @@ // Complete the following underscore functions. // Reference http://underscorejs.org/ for examples. -const keys = (obj) => { +const keys = obj => Object.keys(obj); // Retrieve all the names of the object's properties. // Return the keys as strings in an array. // Based on http://underscorejs.org/#keys -}; -const values = (obj) => { +const values = obj => Object.values(obj); // Return all of the values of the object's own properties. // Ignore functions // http://underscorejs.org/#values -}; const mapObject = (obj, cb) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject + for (let i = 0; i < keys(obj).length; i++) { + obj[keys(obj)[i]] = cb(values(obj)[i]); + } + return obj; }; - const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs + const outArr = []; + for (let i = 0; i < keys(obj).length; i++) { + const subArr = []; + subArr.push(keys(obj)[i], values(obj)[i]); + outArr[i] = subArr; + } + return outArr; }; const invert = (obj) => { // Returns a copy of the object where the keys have become the values and the values the keys. // Assume that all of the object's values will be unique and string serializable. // http://underscorejs.org/#invert + const newObj = {}; + for (let i = 0; i < keys(obj).length; i++) { + newObj[values(obj)[i]] = keys(obj)[i]; + } + return newObj; }; const defaults = (obj, defaultProps) => { // Fill in undefined properties that match properties on the `defaultProps` parameter object. // Return `obj`. // http://underscorejs.org/#defaults + for (let i = 0; i < keys(defaultProps).length; i++) { + if (obj[keys(defaultProps)[i]] === undefined) { + obj[keys(defaultProps)[i]] = values(defaultProps)[i]; + } + } + return obj; }; /* eslint-enable no-unused-vars */ From b3725b0eb3761f323fa29205ef9eda53f83bef11 Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Sat, 9 Sep 2017 10:17:28 -0400 Subject: [PATCH 7/8] Project W1(B) -Arrays: Completed -Class: Completed -Closure: Completed -ES6: Completed -Objects: Completed -'This': Completed --- src/this.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/this.js b/src/this.js index 8ea3020..c25cf89 100644 --- a/src/this.js +++ b/src/this.js @@ -5,27 +5,30 @@ class User { constructor(options) { // set a username and password property on the user object that is created + this.username = options.username; + this.password = options.password; + this.checkPassword = string => string === this.password; } // create a method on the User class called `checkPassword` // this method should take in a string and compare it to the object's password property - // return `true` if they match, otherwise return `false` + // return `true` if they match, otherwise return `false` } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` -const checkPassword = function comparePasswords(passwordToCompare) { +const checkPassword = (passwordToCompare) => this.password === passwordToCompare; // recreate the `checkPassword` method that you made on the `User` class // use `this` to access the object's `password` property. // do not modify this function's parameters // note that we use the `function` keyword and not `=>` -}; + // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind - // .call - +console.log(checkPassword.call(this, User.password)); // .apply - +console.log(checkPassword.apply(this, User.password)); // .bind +console.log(checkPassword.bind(this)()); \ No newline at end of file From b751be4c5ad0515b8875147fe7338ac9d42abb9c Mon Sep 17 00:00:00 2001 From: Brandon Fizer <30492636+Track7Dev@users.noreply.github.com> Date: Sat, 9 Sep 2017 11:54:10 -0400 Subject: [PATCH 8/8] Project W1(B) - Completed -Arrays: Completed -Class: Completed -Closure: Completed -ES6: Completed -Objects: Completed -'This': Completed -Recursion: Completed --- src/recursion.js | 14 +++++++++++++- src/this.js | 9 ++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index eb65c57..0c6627d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,17 +3,29 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence + if (n <= 1) return 1; + return nFibonacci(n - 1) + nFibonacci(n - 2); }; const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` + if (n === 0) return 1; + return n * nFactorial(n - 1); }; - /* Extra Credit */ const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + const checkBox = []; + function checkNested(subObj) { + for (let i = 0; i < Object.keys(subObj).length; i++) { + if (typeof subObj[Object.keys(subObj)[i]] !== 'object') checkBox.push(subObj[Object.keys(subObj)[i]]); + if (typeof subObj[Object.keys(subObj)[i]] === 'object') checkNested(subObj[Object.keys(subObj)[i]]); + } + return checkBox.every(allkeys => allkeys === checkBox[0]); + } + return checkNested(obj); }; /* eslint-enable no-unused-vars */ diff --git a/src/this.js b/src/this.js index c25cf89..7991a96 100644 --- a/src/this.js +++ b/src/this.js @@ -11,18 +11,17 @@ class User { } // create a method on the User class called `checkPassword` // this method should take in a string and compare it to the object's password property - // return `true` if they match, otherwise return `false` + // return `true` if they match, otherwise return `false` } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` -const checkPassword = (passwordToCompare) => this.password === passwordToCompare; +const checkPassword = passwordToCompare => this.password === passwordToCompare; // recreate the `checkPassword` method that you made on the `User` class // use `this` to access the object's `password` property. // do not modify this function's parameters - // note that we use the `function` keyword and not `=>` - + // note that we use the `function` keyword and not `=>' // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind @@ -31,4 +30,4 @@ console.log(checkPassword.call(this, User.password)); // .apply console.log(checkPassword.apply(this, User.password)); // .bind -console.log(checkPassword.bind(this)()); \ No newline at end of file +console.log(checkPassword.bind(this)());