From 8e1daf71fe5613b897fe7042519e2de107e62d14 Mon Sep 17 00:00:00 2001 From: Raymond Date: Thu, 10 Aug 2017 17:27:59 -0400 Subject: [PATCH 1/2] Complete Arrays.js --- src/arrays.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index 26aaed3..df4e777 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -8,33 +8,69 @@ 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 newElements = []; + for (let i = 0; i < elements.length; i++) { + newElements.push(cb(elements[i])); + } + return newElements; }; 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 reduced; + 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 newArray = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) { + newArray.push(elements[i]); + } + } + return newArray; }; const flatten = (elements) => { // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; + //Want to take the array of arrays and find the number + const newArray = []; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === 'number') { + newArray.push(elements[i]); + } else if (typeof elements !== 'number') { + elements = elements.concat(elements[i]); + } + } + return newArray; }; /* eslint-enable no-unused-vars, max-len */ @@ -46,4 +82,4 @@ module.exports = { find, filter, flatten -}; +}; \ No newline at end of file From d6ec211373d5d64d858019ece33688780183f804 Mon Sep 17 00:00:00 2001 From: Raymond Date: Fri, 11 Aug 2017 17:29:27 -0400 Subject: [PATCH 2/2] Semi-complete work --- src/arrays.js | 4 ++-- src/class.js | 27 +++++++++++++++++++++++++++ src/es6.js | 45 +++++++++++++++++++++++---------------------- src/objects.js | 8 +++----- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index df4e777..38e4d5d 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -61,7 +61,7 @@ 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]; - //Want to take the array of arrays and find the number + // Want to take the array of arrays and find the number const newArray = []; for (let i = 0; i < elements.length; i++) { if (typeof elements[i] === 'number') { @@ -82,4 +82,4 @@ module.exports = { find, filter, flatten -}; \ No newline at end of file +}; diff --git a/src/class.js b/src/class.js index 8276e29..fd290d8 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(password) { + return this.password === password; + } +} + /* eslint-disable no-undef */ // Remove this comment once you write your classes. @@ -18,7 +28,24 @@ // 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, diff --git a/src/es6.js b/src/es6.js index eb846ab..bf92379 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,50 +7,51 @@ //---------------- // 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(options) { + this.username = options.username; + this.password = options.password; + this.sayHi = function() { + 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++) { - sum += arguments[i]; +const addArgs = (...args) => { + let sum = 0; + for (let i = 0; i < args.length; i++) { + sum += args[i]; } return sum; }; -var argsToCb = function (cb) { - var args = Array.prototype.slice.call(arguments); - return cb.apply(null, args.splice(1)); +const argsToCb = (cb) => { + return cb(...args); }; -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 */ diff --git a/src/objects.js b/src/objects.js index ba39c6c..aeac752 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,17 +1,15 @@ // 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.