From 9002d57443d25a9dec5f0b9f6c2291653a5326ac Mon Sep 17 00:00:00 2001 From: Dylan Date: Tue, 8 Aug 2017 14:33:46 -0400 Subject: [PATCH 01/19] changed es6 --- src/es6.js | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/es6.js b/src/es6.js index eb846ab..c78c8bd 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,50 +7,52 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +const 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) { - this.username = options.username; - this.password = options.password; - this.sayHi = function() { - return this.username + ' says hello!'; - }; +class User { + constructor(username, password) { + this.username = username; + this.password = password; + this.sayHi = () => { + return '${this.username} says hello!' + } + } } -var username = 'JavaScriptForever'; -var password = 'password'; +const username = 'JavaScriptForever'; +const password = 'password'; -var me = new User({ - username: username, - password: password, +const me = new User({ + username, + password, }); // ---------------- // let, const, =>, ... (spread operator) -var addArgs = function () { - var sum = 0; - for (var i = 0; i < arguments.length; i++) { +const addArgs = () => { + let sum = 0; + for (let i = 0; i < arguments.length; i++) { sum += arguments[i]; } return sum; }; -var argsToCb = function (cb) { - var args = Array.prototype.slice.call(arguments); - return cb.apply(null, args.splice(1)); +const argsToCb = (cb) => { + const args = Array.prototype.slice.call(arguments); + return cb.apply(...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 ea50990f138c9ca9f3e76df4a6a65c8949c9f0a8 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Tue, 8 Aug 2017 16:45:18 -0500 Subject: [PATCH 02/19] arrays.js in progress --- src/arrays.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 26aaed3..6d5e72c 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -8,17 +8,39 @@ 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 arr = []; + for (let i = 0; i < elements.length; i++) { + arr.push(cb(elements[i])); + } + return arr; }; 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. + if (memo) { + cb(memo); + let initialElement = memo; + for (let i = 0; i < elements.length; i++) { + initialElement += elements[i]; + cb(initialElement); + } + } else { + let initialElement = elements[0]; + for (let i = 1; i < elements.length; i++) { + initialElement += elements[i]; + cb(initialElement); + } + } }; const find = (elements, cb) => { From 35d25afe83617e7555d9164ed3d8de58093f3886 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Tue, 8 Aug 2017 17:54:25 -0500 Subject: [PATCH 03/19] arrays.js still in progress, nearly there --- src/arrays.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 6d5e72c..9b3f97d 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -27,20 +27,21 @@ 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. + let initialElement; if (memo) { - cb(memo); - let initialElement = memo; + initialElement = memo; for (let i = 0; i < elements.length; i++) { initialElement += elements[i]; cb(initialElement); } } else { - let initialElement = elements[0]; + initialElement = elements[0]; for (let i = 1; i < elements.length; i++) { initialElement += elements[i]; cb(initialElement); } } + return initialElement; }; const find = (elements, cb) => { From f7cec7fb85ce61532b8d1d318a1e0615b2e86974 Mon Sep 17 00:00:00 2001 From: Dylan Date: Tue, 8 Aug 2017 21:16:22 -0400 Subject: [PATCH 04/19] finished arrays --- src/arrays.js | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 9b3f97d..db67b36 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -27,35 +27,46 @@ 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. - let initialElement; - if (memo) { - initialElement = memo; - for (let i = 0; i < elements.length; i++) { - initialElement += elements[i]; - cb(initialElement); - } - } else { - initialElement = elements[0]; - for (let i = 1; i < elements.length; i++) { - initialElement += elements[i]; - cb(initialElement); - } + let result = memo; + for (let i = 0; i < elements.length; i++) { + result = cb(result, elements[i]); } - return initialElement; + return result; }; const find = (elements, cb) => { + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) { + return elements[i]; + } + } // 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. }; const filter = (elements, cb) => { + const array = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) { + array.push(elements[i]); + } + } + return array; // 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 flatten = (elements) => { + let array = []; + for (let i = 0; i < elements.length; i++) { + if (Array.isArray(elements[i])) { + array = array.concat(flatten(elements[i])); + } else { + array.push(elements[i]); + } + } + return array; // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; }; From 57d170825f791606fd2c4e84838fbdcaf3bb6906 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Wed, 9 Aug 2017 12:41:40 -0500 Subject: [PATCH 05/19] objects.js in progress --- src/objects.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/objects.js b/src/objects.js index ba39c6c..b8fd131 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,12 +5,14 @@ const 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 + return Object.keys(obj); }; const values = (obj) => { // Return all of the values of the object's own properties. // Ignore functions // http://underscorejs.org/#values + return Object.values(obj); }; const mapObject = (obj, cb) => { From b642540d3a6ce57f96ac2da518879f2c1526df6e Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 9 Aug 2017 14:37:11 -0400 Subject: [PATCH 06/19] Completed objects --- src/objects.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/objects.js b/src/objects.js index b8fd131..2f781c2 100644 --- a/src/objects.js +++ b/src/objects.js @@ -18,23 +18,47 @@ const values = (obj) => { const mapObject = (obj, cb) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject + const mapKeys = Object.keys(obj); + mapKeys.forEach((key) => { + obj[key] = cb(obj[key]); + }); + return obj; }; const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs + const mapKeys = Object.keys(obj); + const array = []; + mapKeys.forEach((key) => { + array.push([key, obj[key]]); + }); + return array; }; 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 = {}; + const mapKeys = Object.keys(obj); + mapKeys.forEach((key) => { + newObj[obj[key]] = key; + }); + return newObj; }; const defaults = (obj, defaultProps) => { // Fill in undefined properties that match properties on the `defaultProps` parameter object. // Return `obj`. // http://underscorejs.org/#defaults + const mapKeys = Object.keys(defaultProps); + mapKeys.forEach((prop) => { + if (!obj[prop]) { + obj[prop] = defaultProps[prop]; + } + }); + return obj; }; /* eslint-enable no-unused-vars */ From ad045b7fdfafb57a355a126e438eab34bcac1028 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 9 Aug 2017 16:13:55 -0400 Subject: [PATCH 07/19] completed this --- src/this.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/this.js b/src/this.js index 8ea3020..dc0ac49 100644 --- a/src/this.js +++ b/src/this.js @@ -4,8 +4,16 @@ class User { constructor(options) { + this.username = options.username; + this.password = options.password; // set a username and password property on the user object that is created } + checkPassword(str) { + if(str === this.password) { + return true; + } + return false; + } // 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` @@ -15,6 +23,10 @@ const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatteryst const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` const checkPassword = function comparePasswords(passwordToCompare) { + if (passwordToCompare === this.password) { + return true; + } + return false; // 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 @@ -23,9 +35,10 @@ const checkPassword = function comparePasswords(passwordToCompare) { // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind - +checkPassword.call(me, 'correcthorsebatterystaple'); // .call - +checkPassword.apply(me, ['correcthorsebatterystaple']); // .apply - +const checkMyPassword = checkPassword.bind(me); +checkMyPassword('correcthorsebatterystaple'); // .bind From 2a058c1b10473f201e9d331c86deefd277320871 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 9 Aug 2017 16:38:31 -0400 Subject: [PATCH 08/19] completed class --- src/class.js | 30 +++++++++++++++++++++++++++++- src/this.js | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/class.js b/src/class.js index 8276e29..16ce654 100644 --- a/src/class.js +++ b/src/class.js @@ -5,10 +5,38 @@ // Add a method called `comparePasswords`. `comparePasswords` should have a parameter // for a potential password that will be compared to the `password` property. // Return true if the potential password matches the `password` property. Otherwise return false. - +class User { + constructor(options) { + this.email = options.email; + this.password = options.password; + } + comparePasswords(potentialPassword) { + if (this.password === potentialPassword) { + return true; + } + return false; + } +} /* eslint-disable no-undef */ // Remove this comment once you write your classes. +class Animal { + constructor(options) { + this.age = options.age; + } + growOlder() { + return this.age + 1; + } +} +class Cat extends Animal { + constructor(options) { + super(options); + this.name = options.name; + } + meow() { + return `${this.name} meowed!`; + } +} // Create a class called `Animal` and a class called `Cat`. // `Cat` should extend the `Animal` class. diff --git a/src/this.js b/src/this.js index dc0ac49..072b51c 100644 --- a/src/this.js +++ b/src/this.js @@ -9,7 +9,7 @@ class User { // set a username and password property on the user object that is created } checkPassword(str) { - if(str === this.password) { + if (str === this.password) { return true; } return false; From 896be4867d99afd630a73b57c2b2003f654ec63b Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 9 Aug 2017 20:46:07 -0400 Subject: [PATCH 09/19] worked on closure --- src/closure.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/closure.js b/src/closure.js index 4c98af0..dc5eeac 100644 --- a/src/closure.js +++ b/src/closure.js @@ -1,6 +1,12 @@ // Complete the following functions. const counter = () => { + let count = 0; + const newCounter = () => { + count++; + return count; + }; + return newCounter; // Return a function that when invoked increments and returns a counter variable. // Example: const newCounter = counter(); // newCounter(); // 1 @@ -8,6 +14,21 @@ const counter = () => { }; const counterFactory = () => { + let count = 0; + const newCounter = (value) => { + count += value; + }; + return { + increment() { + newCounter(1); + return count; + }, + decrement() { + newCounter(-1); + return count; + } + }; + // 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. @@ -16,6 +37,14 @@ const counterFactory = () => { const limitFunctionCallCount = (cb, n) => { // Should return a function that invokes `cb`. // The returned function should only allow `cb` to be invoked `n` times. + let count = 0; + return (...args) => { + if (count < n) { + count++; + return cb(...args); + } + return null; + }; }; const cacheFunction = (cb) => { From f1aeed0b66ed4f109ee6fd4a89fb19d5b31dba98 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 10 Aug 2017 14:16:46 -0400 Subject: [PATCH 10/19] completed closure --- src/closure.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/closure.js b/src/closure.js index dc5eeac..c84aa57 100644 --- a/src/closure.js +++ b/src/closure.js @@ -54,6 +54,14 @@ 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 (...args) => { + if (!cache.includes(...args)) { + cache.push(...args); + return cb(...args); + } + return cache; + }; }; /* eslint-enable no-unused-vars */ From 518837da0e81f82a36bdad6fa3d320ba80b591e7 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 10 Aug 2017 18:21:07 -0400 Subject: [PATCH 11/19] Completed AJ --- src/recursion.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/recursion.js b/src/recursion.js index a3e997e..d86dbe2 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,6 +1,12 @@ // Complete the following functions. const nFibonacci = (n) => { + if (n === 0) { + return 1; + } else if (n < 0) { + return 0; + } + return nFibonacci(n - 1) + nFibonacci(n - 2); // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence }; @@ -8,11 +14,27 @@ const nFibonacci = (n) => { const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` + if (n === 1) { + return 1; + } + return n * nFactorial(n - 1); }; + const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + const flat = []; + const recursion = (...args) => { + Object.values(...args).forEach((value) => { + if (typeof value === 'object') { + return recursion(value); + } + flat.push(value); + }); + return flat.every(p => p === flat[0]); + }; + return recursion(obj); }; /* eslint-enable no-unused-vars */ From 2dd65b7cb9f2f1e3a9339280567326ae306219a7 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 10 Aug 2017 19:33:02 -0400 Subject: [PATCH 12/19] made changes to es6 --- src/es6.js | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/es6.js b/src/es6.js index c78c8bd..9c8aa07 100644 --- a/src/es6.js +++ b/src/es6.js @@ -9,10 +9,7 @@ const food = 'pineapple'; -const isMyFavoriteFood = (food) => { - food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey - return food === 'thousand-year-old egg'; -}; +const isMyFavoriteFood = (food = 'thousand-year-old egg') => (food === 'thousand-year-old egg'); //This sets a default value if `food` is falsey const isThisMyFavorite = isMyFavoriteFood(food); @@ -20,13 +17,13 @@ const isThisMyFavorite = isMyFavoriteFood(food); //const, class, template literals, enhanced object literals (foo: foo, -> foo,) class User { - constructor(username, password) { - this.username = username; - this.password = password; - this.sayHi = () => { - return '${this.username} says hello!' - } + constructor(options) { + this.username = options.username; + this.password = options.password; } + sayHi() { + return `${this.username} says hello!` + } } const username = 'JavaScriptForever'; @@ -40,18 +37,9 @@ const me = new User({ // ---------------- // let, const, =>, ... (spread operator) -const addArgs = () => { - let sum = 0; - for (let i = 0; i < arguments.length; i++) { - sum += arguments[i]; - } - return sum; -}; - -const argsToCb = (cb) => { - const args = Array.prototype.slice.call(arguments); - return cb.apply(...args.splice(1)); -}; +const addArgs = (...args) => (args.reduce((memo, val) => (memo + val))); + +const argsToCb = (cb, ...args) => (cb(...args)); const result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 From aff0b73310bc17eead4b8563e53b74dbc1f11df0 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Aug 2017 14:24:29 -0400 Subject: [PATCH 13/19] optimized objects code --- src/objects.js | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/objects.js b/src/objects.js index 2f781c2..9319f96 100644 --- a/src/objects.js +++ b/src/objects.js @@ -18,21 +18,15 @@ const values = (obj) => { const mapObject = (obj, cb) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject - const mapKeys = Object.keys(obj); - mapKeys.forEach((key) => { - obj[key] = cb(obj[key]); - }); + keys(obj).forEach(k => obj[k] = cb(obj[k])); return obj; }; const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs - const mapKeys = Object.keys(obj); const array = []; - mapKeys.forEach((key) => { - array.push([key, obj[key]]); - }); + keys(obj).forEach(k => array.push([k, obj[k]])); return array; }; @@ -40,24 +34,16 @@ 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 = {}; - const mapKeys = Object.keys(obj); - mapKeys.forEach((key) => { - newObj[obj[key]] = key; - }); - return newObj; + const inv = {}; + keys(obj).forEach(k => inv[obj[k]] = k); + return inv; }; const defaults = (obj, defaultProps) => { // Fill in undefined properties that match properties on the `defaultProps` parameter object. // Return `obj`. // http://underscorejs.org/#defaults - const mapKeys = Object.keys(defaultProps); - mapKeys.forEach((prop) => { - if (!obj[prop]) { - obj[prop] = defaultProps[prop]; - } - }); + Object.keys(defaultProps).forEach((d) => { if (!obj[d]) obj[d] = defaultProps[d]; }); return obj; }; From 277b72e76c4d799c86ea7dafab23a3852f340b89 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Aug 2017 14:43:38 -0400 Subject: [PATCH 14/19] made improvements to this --- src/this.js | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/this.js b/src/this.js index 072b51c..9b50b49 100644 --- a/src/this.js +++ b/src/this.js @@ -4,41 +4,36 @@ 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; - // set a username and password property on the user object that is created - } - checkPassword(str) { - if (str === this.password) { - return true; - } - return false; } // 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` + checkPassword(str) { if (str === this.password) return true; + return false; + }; } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` const checkPassword = function comparePasswords(passwordToCompare) { - if (passwordToCompare === this.password) { - return true; - } - return false; // 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 `=>` + if (passwordToCompare === this.password) return true; + return false; }; // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind -checkPassword.call(me, 'correcthorsebatterystaple'); +checkPassword.call(me, this.password); // .call -checkPassword.apply(me, ['correcthorsebatterystaple']); +checkPassword.apply(me, [this.password]); // .apply const checkMyPassword = checkPassword.bind(me); -checkMyPassword('correcthorsebatterystaple'); +checkMyPassword(this.password); // .bind From 3a6e2a191fd959cb1abb559f9226d9922f80a43a Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Aug 2017 15:00:05 -0400 Subject: [PATCH 15/19] syntax improvements --- src/this.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/this.js b/src/this.js index 9b50b49..1023b5b 100644 --- a/src/this.js +++ b/src/this.js @@ -11,9 +11,10 @@ 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` - checkPassword(str) { if (str === this.password) return true; + checkPassword(str) { + if (str === this.password) return true; return false; - }; + } } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); From 1e425785e1eeb4d04894e1087380aff2feadaa91 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Aug 2017 15:00:38 -0400 Subject: [PATCH 16/19] syntax changes for class --- src/class.js | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/class.js b/src/class.js index 16ce654..aee07ae 100644 --- a/src/class.js +++ b/src/class.js @@ -10,22 +10,26 @@ class User { this.email = options.email; this.password = options.password; } - comparePasswords(potentialPassword) { - if (this.password === potentialPassword) { - return true; - } + comparePasswords(str) { + if (str === this.password) return true; return false; } } -/* eslint-disable no-undef */ // Remove this comment once you write your classes. + +// Create a class called `Animal` and a class called `Cat`. +// `Cat` should extend the `Animal` class. +// Animal and Cat should both have a parameter called `options` in their constructors. +// Animal should have the property `age` that's set in the constructor and the method +// `growOlder` that returns the age. +// Cat should have the property `name` that is set in the constructor and the method +// `meow` that should return the string ` meowed!` where `` is the `name` +// property set on the Cat instance. class Animal { constructor(options) { this.age = options.age; } - growOlder() { - return this.age + 1; - } + growOlder() { return this.age + 1; } } class Cat extends Animal { @@ -33,21 +37,9 @@ class Cat extends Animal { super(options); this.name = options.name; } - meow() { - return `${this.name} meowed!`; - } + meow() { return `${this.name} meowed!`; } } -// Create a class called `Animal` and a class called `Cat`. -// `Cat` should extend the `Animal` class. -// Animal and Cat should both have a parameter called `options` in their constructors. -// Animal should have the property `age` that's set in the constructor and the method -// `growOlder` that returns the age. -// Cat should have the property `name` that is set in the constructor and the method -// `meow` that should return the string ` meowed!` where `` is the `name` -// property set on the Cat instance. - - module.exports = { User, Cat From 5ab69c011a647fedddb348e0bb64fd7ca39e7f87 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Aug 2017 17:49:40 -0400 Subject: [PATCH 17/19] improved closure code --- src/closure.js | 42 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/src/closure.js b/src/closure.js index c84aa57..16df815 100644 --- a/src/closure.js +++ b/src/closure.js @@ -1,37 +1,23 @@ // Complete the following functions. const counter = () => { - let count = 0; - const newCounter = () => { - count++; - return count; - }; - return newCounter; // Return a function that when invoked increments and returns a counter variable. // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + return () => ++count; }; const counterFactory = () => { - let count = 0; - const newCounter = (value) => { - count += value; - }; - return { - increment() { - newCounter(1); - return count; - }, - decrement() { - newCounter(-1); - return count; - } - }; - // 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. + let count = 0; + return { + increment() { return ++count; }, + decrement() { return --count; } + }; }; const limitFunctionCallCount = (cb, n) => { @@ -39,10 +25,7 @@ const limitFunctionCallCount = (cb, n) => { // The returned function should only allow `cb` to be invoked `n` times. let count = 0; return (...args) => { - if (count < n) { - count++; - return cb(...args); - } + if (count < n) { count++; return cb(...args); } return null; }; }; @@ -56,14 +39,11 @@ const cacheFunction = (cb) => { // `cb` should only ever be invoked once for a given set of arguments. const cache = []; return (...args) => { - if (!cache.includes(...args)) { - cache.push(...args); - return cb(...args); - } - return cache; + if (cache.includes(...args)) return cache; + cache.push(...args); + return cb(...args); }; }; - /* eslint-enable no-unused-vars */ module.exports = { From 260940ff641e78b327bcea9b6b86bf917e3dcad4 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Aug 2017 19:22:22 -0400 Subject: [PATCH 18/19] improved recursion --- src/recursion.js | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index d86dbe2..50ffd7d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,40 +1,28 @@ // Complete the following functions. const nFibonacci = (n) => { - if (n === 0) { - return 1; - } else if (n < 0) { - return 0; - } - return nFibonacci(n - 1) + nFibonacci(n - 2); // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence + return n <= 1 ? 1 : nFibonacci(n - 1) + nFibonacci(n - 2); }; const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` - if (n === 1) { - return 1; - } - return n * nFactorial(n - 1); + return n <= 1 ? 1 : n * nFactorial(n - 1); }; - const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false - const flat = []; - const recursion = (...args) => { - Object.values(...args).forEach((value) => { - if (typeof value === 'object') { - return recursion(value); - } - flat.push(value); + const array = []; + const rf = (...args) => { + Object.values(...args).forEach((v) => { + return typeof v === 'object' ? rf(v) : array.push(v); }); - return flat.every(p => p === flat[0]); }; - return recursion(obj); + rf(obj); + return new Set(array).size === 1; }; /* eslint-enable no-unused-vars */ From 7b1f3bd5457ec5749b28fc9f8cc2fe23b92ce60b Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Aug 2017 20:01:41 -0400 Subject: [PATCH 19/19] improved flatten --- src/arrays.js | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index db67b36..75dd384 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -36,9 +36,7 @@ const reduce = (elements, cb, memo = elements.shift()) => { const find = (elements, cb) => { for (let i = 0; i < elements.length; i++) { - if (cb(elements[i]) === true) { - return elements[i]; - } + if (cb(elements[i]) === true) return elements[i]; } // Look through each value in `elements` and pass each element to `cb`. // If `cb` returns `true` then return that element. @@ -48,9 +46,7 @@ const find = (elements, cb) => { const filter = (elements, cb) => { const array = []; for (let i = 0; i < elements.length; i++) { - if (cb(elements[i]) === true) { - array.push(elements[i]); - } + if (cb(elements[i]) === true) array.push(elements[i]); } return array; // Similar to `find` but you will return an array of all elements that passed the truth test @@ -58,17 +54,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]; let array = []; for (let i = 0; i < elements.length; i++) { - if (Array.isArray(elements[i])) { - array = array.concat(flatten(elements[i])); - } else { - array.push(elements[i]); - } + const roller = Array.isArray(elements[i]) ? flatten(elements[i]) : [elements[i]]; + array = array.concat(roller); } return array; - // Flattens a nested array (the nesting can be to any depth). - // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; }; /* eslint-enable no-unused-vars, max-len */