From 56decafce154f8417a033892825caeb12506e315 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Thu, 19 Oct 2017 14:04:09 -0400 Subject: [PATCH 01/20] var to const, start over from 0 --- src/es6.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/es6.js b/src/es6.js index eb846ab..f72d0b4 100644 --- a/src/es6.js +++ b/src/es6.js @@ -8,6 +8,8 @@ // const, =>, default parameters, arrow functions default return statements using () var food = 'pineapple'; +// var changes to const +const food = 'pineapple'; var isMyFavoriteFood = function(food) { food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey From 557b785456635f0c54a56f238e197d55e1286223 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Fri, 20 Oct 2017 17:31:46 -0400 Subject: [PATCH 02/20] each 1st test passed, working on es6 --- src/arrays.js | 4 ++++ src/es6.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index f24d6ef..3395bf3 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,6 +6,10 @@ 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]); + } }; const map = (elements, cb) => { diff --git a/src/es6.js b/src/es6.js index f72d0b4..33c1d05 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,7 +7,7 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +//var food = 'pineapple'; // var changes to const const food = 'pineapple'; From b6cd71318b53c753e7294fd7e49e57e101f74b9d Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Fri, 20 Oct 2017 17:33:01 -0400 Subject: [PATCH 03/20] second each test passed index correctly --- src/arrays.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index 3395bf3..06c9ac6 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -8,7 +8,7 @@ const each = (elements, cb) => { // based off http://underscorejs.org/#each for (let i = 0; i < elements.length; i++) { - cb(elements[i]); + cb(elements[i], i); } }; From e44d949d3daaeea8fc4a2193e3ced2d8af0c17cd Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Fri, 20 Oct 2017 17:36:58 -0400 Subject: [PATCH 04/20] one test in map passing, need to pass elements correctly --- src/arrays.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 06c9ac6..caba40c 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -15,6 +15,12 @@ const each = (elements, cb) => { 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 newArray = []; + for (let i = 0; i < elements.length; i++) { + newArray.push(elements[i]); + } + return newArray; }; const reduce = (elements, cb, memo) => { From 4bdf9157e2d09b3d754b290f2260defff521e07b Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Fri, 20 Oct 2017 17:41:35 -0400 Subject: [PATCH 05/20] map working, push elements correctly --- src/arrays.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index caba40c..3e7e826 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -16,11 +16,11 @@ 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 newArray = []; + const resultArray = []; for (let i = 0; i < elements.length; i++) { - newArray.push(elements[i]); + resultArray.push(cb(elements[i])); } - return newArray; + return resultArray; }; const reduce = (elements, cb, memo) => { From b589fc492f7c2145f09615da77e6097806d0376f Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Mon, 23 Oct 2017 15:56:09 -0400 Subject: [PATCH 06/20] added forEach version of map, left in for loop commented out --- src/arrays.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 3e7e826..bee17ec 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -17,9 +17,13 @@ const map = (elements, cb) => { // Return the new array. const resultArray = []; - for (let i = 0; i < elements.length; i++) { - resultArray.push(cb(elements[i])); - } + // for (let i = 0; i < elements.length; i++) { + // resultArray.push(cb(elements[i])); + // THIS IS THE for loop version (above) + // THIS IS THE forEach version (below) + each(elements, (elem) => { + resultArray.push(cb(elem)); + }); return resultArray; }; From 9c59e8823d20d459f518e99039cf18d13f8c0d0a Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Mon, 23 Oct 2017 20:42:47 -0400 Subject: [PATCH 07/20] reduce passes tests --- src/arrays.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index bee17ec..779962e 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -20,6 +20,7 @@ const map = (elements, cb) => { // for (let i = 0; i < elements.length; i++) { // resultArray.push(cb(elements[i])); // THIS IS THE for loop version (above) + // // THIS IS THE forEach version (below) each(elements, (elem) => { resultArray.push(cb(elem)); @@ -31,12 +32,26 @@ const reduce = (elements, cb, memo) => { // 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. + // + // for(let i = 0; i < elements.length; i++){ + if (memo === undefined) { + memo = elements.shift(); + } + each(elements, (elem) => { + memo = cb(memo, elem); + }); + 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. + each(elements, (elem) => { + if (cb[elem] === true) { + return true; + } + }); }; const filter = (elements, cb) => { From d1809416f0fe71819e4bb860a0815b91c23326e5 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Mon, 23 Oct 2017 20:51:19 -0400 Subject: [PATCH 08/20] reduce passes test, and does not modify original array --- src/arrays.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 779962e..8a1d48f 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -34,12 +34,14 @@ const reduce = (elements, cb, memo) => { // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value. // // for(let i = 0; i < elements.length; i++){ + let i = 0; if (memo === undefined) { - memo = elements.shift(); + memo = elements[0]; + i = 1; + } + for (; i < elements.length; i++) { + memo = cb(memo, elements[i]); } - each(elements, (elem) => { - memo = cb(memo, elem); - }); return memo; }; From 995e9cbc5db21052d653d03e4fe6fdcc1dcbad5f Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Mon, 23 Oct 2017 23:20:52 -0400 Subject: [PATCH 09/20] flatten array without nested array passes --- src/arrays.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 8a1d48f..c847201 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -64,6 +64,18 @@ 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 result = []; + each(elements, (elem) => { + if (Array.isArray(elem)) { + const nestedArray = elem; + each(nestedArray, (nestedElem) => { + result.push(nestedElem); + }); + } else { + result.push(elem); + } + }); + return result; }; /* eslint-enable no-unused-vars, max-len */ From 48656fafac5e853f34707a6dff86d0fe47f6d896 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Mon, 23 Oct 2017 23:57:50 -0400 Subject: [PATCH 10/20] flatten array passes all 3 tests --- src/arrays.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index c847201..c0ea324 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -62,6 +62,12 @@ const filter = (elements, cb) => { }; const flatten = (elements) => { + elements = map(elements, (elem) => { + if (Array.isArray(elem)) { + return flatten(elem); + } + return elem; + }); // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; const result = []; From f2876d5cadb3b0d271722e277e211a32f9eadcc2 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 00:18:20 -0400 Subject: [PATCH 11/20] flatten array refactored, recursive call flatten within flatten function --- src/arrays.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index c0ea324..b341a44 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -62,21 +62,34 @@ const filter = (elements, cb) => { }; const flatten = (elements) => { - elements = map(elements, (elem) => { - if (Array.isArray(elem)) { - return flatten(elem); - } - return elem; - }); // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; - const result = []; + // +// elements = map(elements, (elem) => { +// if (Array.isArray(elem)) { +// return flatten(elem); +// } +// return elem; +// }); +// const result = []; +// each(elements, (elem) => { +// if (Array.isArray(elem)) { +// const nestedArray = elem; +// each(nestedArray, (nestedElem) => { +// result.push(nestedElem); +// }); +// } else { +// result.push(elem); +// } +// }); +// return result; +// }; +// or + let result = []; each(elements, (elem) => { if (Array.isArray(elem)) { - const nestedArray = elem; - each(nestedArray, (nestedElem) => { - result.push(nestedElem); - }); + const nestedArray = flatten(elem); + result = result.concat(nestedArray); } else { result.push(elem); } From 5ea77690c8c917255d8c81327a346156f53d21f9 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 00:34:30 -0400 Subject: [PATCH 12/20] find works, some trouble using each instead of for loop --- src/arrays.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index b341a44..6dccbd9 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -49,11 +49,11 @@ 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. - each(elements, (elem) => { - if (cb[elem] === true) { - return true; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) { + return elements[i]; } - }); + } }; const filter = (elements, cb) => { From 44f6592a7e6a5623a850eefc1bd18aab8dca0eb8 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 00:37:19 -0400 Subject: [PATCH 13/20] filter works, ty patrick --- src/arrays.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 6dccbd9..40cbed0 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -59,6 +59,13 @@ const find = (elements, cb) => { 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 trueArray = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) { + trueArray.push(elements[i]); + } + } + return trueArray; }; const flatten = (elements) => { From ec2e1acf56f9c1ab0591b2da6e568468a228a74d Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 00:46:55 -0400 Subject: [PATCH 14/20] starting on objects, keys are easy --- src/arrays.js | 2 +- src/objects.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index 40cbed0..624df6b 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -59,7 +59,7 @@ const find = (elements, cb) => { 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 trueArray = []; + const trueArray = []; for (let i = 0; i < elements.length; i++) { if (cb(elements[i]) === true) { trueArray.push(elements[i]); diff --git a/src/objects.js b/src/objects.js index ba39c6c..168e530 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,6 +5,7 @@ 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) => { From 55bdb5a71096a05e6c38721fbfd744d4c7a6142b Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 00:59:24 -0400 Subject: [PATCH 15/20] starting on objects, values were not easy. many failed tries, map worked --- src/objects.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/objects.js b/src/objects.js index 168e530..4450d0a 100644 --- a/src/objects.js +++ b/src/objects.js @@ -12,6 +12,10 @@ const values = (obj) => { // Return all of the values of the object's own properties. // Ignore functions // http://underscorejs.org/#values + const vals = Object.keys(obj).map((key) => { + return obj[key]; + }); + return vals; }; const mapObject = (obj, cb) => { From 8958a382879773129459c793ba4c1fd81c2999ed Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 01:07:43 -0400 Subject: [PATCH 16/20] mapObject all set, create object and pass into newObj --- src/objects.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/objects.js b/src/objects.js index 4450d0a..f5f8a6d 100644 --- a/src/objects.js +++ b/src/objects.js @@ -21,6 +21,11 @@ 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 newObj = {}; + Object.keys(obj).forEach((key) => { + newObj[key] = cb(obj[key]); + }); + return newObj; }; const pairs = (obj) => { From 118cbf5cd2f9f3cde44b2e187b8e023b7723fa82 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 01:27:27 -0400 Subject: [PATCH 17/20] closure counter example done, albeit out of order --- src/closure.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/closure.js b/src/closure.js index 4c98af0..b7324f0 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,7 +5,13 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + return () => { + count++; + return count; + }; }; +const counterFunction = counter(); const counterFactory = () => { // Return an object that has two methods called `increment` and `decrement`. From 4da1bcbd60dafc08a83fb9443a115b317e26cc5b Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 01:49:02 -0400 Subject: [PATCH 18/20] object pairs and invert completed, invert similar to map just setting key instead --- src/objects.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/objects.js b/src/objects.js index f5f8a6d..bb0073e 100644 --- a/src/objects.js +++ b/src/objects.js @@ -16,6 +16,8 @@ const values = (obj) => { return obj[key]; }); return vals; + // return Object.values(obj); + // coming up not a function but clearly is and should pass }; const mapObject = (obj, cb) => { @@ -30,13 +32,25 @@ const mapObject = (obj, cb) => { const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. - // http://underscorejs.org/#pairs + // http://underscorejs.org/# + const pairArray = []; + Object.keys(obj).forEach((key) => { + const nestPairArr = []; + nestPairArr.push(key, obj[key]); + pairArray.push(nestPairArr); + }); + return pairArray; }; 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 = {}; + Object.keys(obj).forEach((key) => { + newObj[obj[key]] = key; + }); + return newObj; }; const defaults = (obj, defaultProps) => { From 6d414a77890a1500e5a1b09d12957a54cb66254f Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 01:50:40 -0400 Subject: [PATCH 19/20] objects completed --- src/objects.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/objects.js b/src/objects.js index bb0073e..f564745 100644 --- a/src/objects.js +++ b/src/objects.js @@ -57,6 +57,13 @@ const defaults = (obj, defaultProps) => { // Fill in undefined properties that match properties on the `defaultProps` parameter object. // Return `obj`. // http://underscorejs.org/#defaults + Object.keys(defaultProps).forEach((key) => { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + return; + } + obj[key] = defaultProps[key]; + }); + return obj; }; /* eslint-enable no-unused-vars */ From 05d4a3faeb9e766bae52050949b0a9460cc202f7 Mon Sep 17 00:00:00 2001 From: JCamp-12 Date: Tue, 24 Oct 2017 10:56:27 -0400 Subject: [PATCH 20/20] classes work, cat extends animal --- src/class.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/class.js b/src/class.js index 8276e29..37be978 100644 --- a/src/class.js +++ b/src/class.js @@ -6,6 +6,16 @@ // 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(pw) { + if (pw === this.password) return true; + return false; + } +} /* eslint-disable no-undef */ // Remove this comment once you write your classes. @@ -14,11 +24,29 @@ // `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. +// `growOlder` that returns the age. + a year. // 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; + } +} + +class Cat extends Animal { + constructor(options) { + super(options); + this.name = options.name; + } + meow() { + return `${this.name} meowed!`; + } +} module.exports = { User,