From 3c9b7bd1b5b3a14d24c908b431e5d5fa1f10dab3 Mon Sep 17 00:00:00 2001 From: Colombianito66 Date: Wed, 12 Jul 2017 19:38:37 -0400 Subject: [PATCH 1/7] es6 refactor complete --- src/es6.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/es6.js b/src/es6.js index eb846ab..9fbba36 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,50 +7,53 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +let food = 'pineapple'; // false -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); +let isThisMyFavorite = isMyFavoriteFood(food); //---------------- //const, class, template literals, enhanced object literals (foo: foo, -> foo,) -var User = function(options) { + +class User { + constructor(options) { this.username = options.username; this.password = options.password; - this.sayHi = function() { - return this.username + ' says hello!'; - }; } + sayHi() { + return `${this.username} says hello!`; + }; +} -var username = 'JavaScriptForever'; -var password = 'password'; +let username = 'JavaScriptForever'; +let 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) => { + let args = Array.prototype.slice.call(arguments); + return cb(...args); }; -var result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 +let result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 /* eslint-enable */ From 952038a4eeaf064be38484d00b5468e5ae5872a6 Mon Sep 17 00:00:00 2001 From: Nana Owusu Nyamekye Date: Thu, 13 Jul 2017 03:15:27 +0000 Subject: [PATCH 2/7] Halfway Through 'arrays.js' --- src/arrays.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index f24d6ef..80e5abe 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,23 +6,56 @@ 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 + + elements.forEach((item, index) => { + cb(item, index); + }); }; 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 myArray = elements.map((item, index) => { + return cb(item, index); + }); + + return myArray; }; 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. + + // let sum; + // if (memo === undefined) sum = elements[0]; + // memo.reduce((sum, elements) => { + // return cb(elements); + // }); + + // if (memo === undefined) { + // memo = elements[0]; + // } + // for (let i = 0; i < memo.length; i++) { + // memo.push.cb(elements); + // } + if (!memo) { + 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]) === true) return elements[i]; + return 'undefined'; }; const filter = (elements, cb) => { From 5c10b6238e732b2c2d1eac71e32b7988d65424fc Mon Sep 17 00:00:00 2001 From: Colombianito66 Date: Thu, 13 Jul 2017 17:13:00 -0400 Subject: [PATCH 3/7] still have two reduce method tests not passing --- src/arrays.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 80e5abe..71bcc50 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -61,11 +61,23 @@ 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 arr = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) arr.push(elements[i]); + } + return arr; }; 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 arr = []; + for (let i = 0; i < elements.length; i++) { + if (Array.isArray(elements[i])) { + arr = arr.concat(flatten(elements[i])); + } else arr.push(elements[i]); + } return arr; }; /* eslint-enable no-unused-vars, max-len */ From 47a2a4586b2c524e5d2b77be92670ae8fb227a38 Mon Sep 17 00:00:00 2001 From: Nana Owusu Nyamekye Date: Thu, 13 Jul 2017 22:08:27 +0000 Subject: [PATCH 4/7] Solved 'class.js' --- src/class.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/class.js b/src/class.js index 8276e29..414acd1 100644 --- a/src/class.js +++ b/src/class.js @@ -5,9 +5,20 @@ // 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(myPassword) { + if (this.password === myPassword) return true; + return false; + } +} -/* eslint-disable no-undef */ // Remove this comment once you write your classes. + + // Remove this comment once you write your classes. // Create a class called `Animal` and a class called `Cat`. @@ -19,6 +30,27 @@ // `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, From e74af509126b903e895c4112ae31bf84913f463b Mon Sep 17 00:00:00 2001 From: Colombianito66 Date: Thu, 13 Jul 2017 21:40:57 -0400 Subject: [PATCH 5/7] objects work in progress --- src/arrays.js | 19 ++++++++++++++----- src/objects.js | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 71bcc50..4754efb 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,7 +6,13 @@ 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 - + if (Array.isArray(elements)) { + for (let i = 0; i < elements.length; i++) { + cb(elements[i], i); + } + } else { + throw new Error(); + } elements.forEach((item, index) => { cb(item, index); }); @@ -15,10 +21,13 @@ 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 myArray = elements.map((item, index) => { - return cb(item, index); + const myArray = []; + each(elements, (element, index) => { + myArray.push(cb(element, index)); }); + // const myArray = elements.map((item, index) => { + // return cb(item, index); + // }); return myArray; }; @@ -41,7 +50,7 @@ const reduce = (elements, cb, memo) => { // memo.push.cb(elements); // } if (!memo) { - memo = elements[0]; + memo = elements.shift(); } for (let i = 0; i < elements.length; i++) { memo = cb(memo, elements[i]); diff --git a/src/objects.js b/src/objects.js index ba39c6c..1f7ccb4 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,21 +1,46 @@ // Complete the following underscore functions. // Reference http://underscorejs.org/ for examples. +// Object.keys(obj).forEach((key) => {}); +// const testObj = { +// name: 'steve', +// age: 12, +// food: 'pizza' +// }; 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 + const keysString = []; + Object.keys(obj).forEach((key) => { + keysString.push(key); + }); + return keysString; }; +// console.log(keys(testObj)); + const values = (obj) => { // Return all of the values of the object's own properties. // Ignore functions // http://underscorejs.org/#values + // QUESTION: return Object.keys(obj).forEach(obj[keys]); + const keysString = []; + Object.keys(obj).forEach((key) => { + keysString.push(obj[key]); + }); + return keysString; }; const mapObject = (obj, cb) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject + //Loop over the elements and call them back + //to the callback then return the elements in a new array. + const newArr = obj.forEach(cb() => { + cb(obj); + }); + return }; const pairs = (obj) => { From 6a9de3191cbaa78216bd242fdbce5fcb131de4ed Mon Sep 17 00:00:00 2001 From: Colombianito66 Date: Tue, 18 Jul 2017 19:03:47 -0400 Subject: [PATCH 6/7] progress --- src/closure.js | 20 ++++++++++++++++++++ src/objects.js | 16 ++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/closure.js b/src/closure.js index 4c98af0..0c8b0ed 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,17 +5,35 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + return () => ++count; }; 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. + let count = 0; + + return { + increment: () => { + return ++count; + }, + decrement: () => { + return --count; + } + }; }; 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 (x) => { + count++; + if (n >= count) return cb(x); + return null; + }; }; const cacheFunction = (cb) => { @@ -25,6 +43,8 @@ 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 = {}; }; /* eslint-enable no-unused-vars */ diff --git a/src/objects.js b/src/objects.js index 1f7ccb4..c307338 100644 --- a/src/objects.js +++ b/src/objects.js @@ -35,12 +35,16 @@ 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 - //Loop over the elements and call them back - //to the callback then return the elements in a new array. - const newArr = obj.forEach(cb() => { - cb(obj); - }); - return + // Loop over the elements and call them back + // to the callback then return the elements in a new array. + // const newArr = obj.forEach(cb() => { + // cb(obj); + // }); + // return + // const arrKeys = keys(obj); + // const arrValues = values(obj); + // for (let i = 0; i < arrValues.length; i++) { + // } }; const pairs = (obj) => { From fb8920ddeb1b7742215ab8cb0d947963a7e52c00 Mon Sep 17 00:00:00 2001 From: Colombianito66 Date: Fri, 21 Jul 2017 18:58:06 -0400 Subject: [PATCH 7/7] progress --- src/arrays.js | 2 +- src/index.html | 0 src/recursion.js | 8 +++++++- src/scripts.js | 0 src/styles.css | 0 src/this.js | 11 +++++++++++ 6 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/index.html create mode 100644 src/scripts.js create mode 100644 src/styles.css diff --git a/src/arrays.js b/src/arrays.js index 4754efb..3b372ee 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -77,7 +77,7 @@ const filter = (elements, cb) => { } return arr; }; - +// flatten rearranges a nested array const flatten = (elements) => { // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..e69de29 diff --git a/src/recursion.js b/src/recursion.js index a3e997e..307f886 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,16 +3,22 @@ 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 < 2) return 1; + return n * (nFactorial(n - 1)); }; - +// checkMatchingLeaves is very similar to 'contains' method in the tree data structure. const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + + }; /* eslint-enable no-unused-vars */ diff --git a/src/scripts.js b/src/scripts.js new file mode 100644 index 0000000..e69de29 diff --git a/src/styles.css b/src/styles.css new file mode 100644 index 0000000..e69de29 diff --git a/src/this.js b/src/this.js index 8ea3020..4d32e00 100644 --- a/src/this.js +++ b/src/this.js @@ -5,10 +5,18 @@ 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 } // 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(string) { + if (string === this.password) { + return true; + } return false; + } } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); @@ -19,6 +27,9 @@ const checkPassword = function comparePasswords(passwordToCompare) { // 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