From ecf5863661090eaf0a0925112882b1f8021a9ef6 Mon Sep 17 00:00:00 2001 From: Allan Felipe Date: Thu, 13 Jul 2017 17:43:18 -0300 Subject: [PATCH 1/6] Added arrays files --- src/arrays.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index f24d6ef..2070280 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,33 +6,71 @@ 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((t, i) => { + cb(t, 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 newarray = []; + elements.forEach((t) => { + newarray.push(cb(t)); + }); + return newarray; }; -const reduce = (elements, cb, memo) => { +const reduce = (elements, cb, memo = elements[0]) => { // 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. + // + // should iteerate over all elements + if (memo === elements[0]) elements.shift(); + elements.forEach((t) => { + memo = cb(memo, t); + }); + 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. + let result; + elements.forEach((t) => { + if (cb(t) === true) { + result = t; + return; + } + }); + return result; }; 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 result = []; + elements.forEach((t) => { + if (cb(t) === true) { + result.push(t); + } + }); + return result; }; 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 set = new Set(arr); + const arreturn = []; + elements.forEach((t) => { + let element = t; + while (typeof element === 'object') { + element = element[0]; + } + arreturn.push(element); + }); + return arreturn; }; /* eslint-enable no-unused-vars, max-len */ From 9be2842ecdfa6c32e9dc4c70b1f153e2d0eeae07 Mon Sep 17 00:00:00 2001 From: Allan Felipe Date: Thu, 13 Jul 2017 17:49:13 -0300 Subject: [PATCH 2/6] Changed array name. --- src/arrays.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 2070280..134ca7b 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -62,7 +62,7 @@ const filter = (elements, cb) => { const flatten = (elements) => { // let set = new Set(arr); - const arreturn = []; + const flattenedArray = []; elements.forEach((t) => { let element = t; while (typeof element === 'object') { @@ -70,7 +70,7 @@ const flatten = (elements) => { } arreturn.push(element); }); - return arreturn; + return flattenedArray; }; /* eslint-enable no-unused-vars, max-len */ From bf3a54e846172c9feaec4dc6dd06d8bf0f7c371e Mon Sep 17 00:00:00 2001 From: Allan Felipe Date: Thu, 13 Jul 2017 17:54:38 -0300 Subject: [PATCH 3/6] Adjust while, to check if array is array instead typof --- src/arrays.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 134ca7b..7fe5a2f 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -63,9 +63,9 @@ const filter = (elements, cb) => { const flatten = (elements) => { // let set = new Set(arr); const flattenedArray = []; - elements.forEach((t) => { - let element = t; - while (typeof element === 'object') { + elements.forEach((item) => { + let element = item; + while (Array.isArray(element) === true) { element = element[0]; } arreturn.push(element); From 0ac0df532a0c922c9d1c7b0c4665bcd10bf0d0ff Mon Sep 17 00:00:00 2001 From: Allan Felipe Date: Thu, 13 Jul 2017 20:07:06 -0300 Subject: [PATCH 4/6] Done class --- src/arrays.js | 2 +- src/class.js | 28 +++++++++++++++++++++++++--- src/recursion.js | 2 +- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 7fe5a2f..81f53f0 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -68,7 +68,7 @@ const flatten = (elements) => { while (Array.isArray(element) === true) { element = element[0]; } - arreturn.push(element); + flattenedArray.push(element); }); return flattenedArray; }; diff --git a/src/class.js b/src/class.js index 8276e29..5aaaea5 100644 --- a/src/class.js +++ b/src/class.js @@ -5,11 +5,33 @@ // 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(potential) { return potential === this.password; } +} +class Animal { + constructor(options) { + this.age = options.age; + } + growOlder() { + return this.age; + } +} +class Cat extends Animal { + constructor(options) { + super(options); + this.name = options.name; + } + meow() { + return this.name.concat(' meowed!'); + } +} -/* eslint-disable no-undef */ // Remove this comment once you write your classes. - - +// eslint-disable no-undef // 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. diff --git a/src/recursion.js b/src/recursion.js index a3e997e..02513ea 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -15,7 +15,7 @@ const checkMatchingLeaves = (obj) => { // otherwise return false }; -/* eslint-enable no-unused-vars */ +/* eslint-enable no-unused-vars*/ module.exports = { nFibonacci, From 1f8893594fd7e075d620a17d7dc519c3b53149cd Mon Sep 17 00:00:00 2001 From: Allan Felipe Date: Thu, 13 Jul 2017 23:20:19 -0300 Subject: [PATCH 5/6] THis+closure --- src/closure.js | 29 +++++++++++++++++++++++++---- src/this.js | 8 +++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/closure.js b/src/closure.js index 4c98af0..f117317 100644 --- a/src/closure.js +++ b/src/closure.js @@ -1,21 +1,39 @@ // Complete the following functions. const counter = () => { - // Return a function that when invoked increments and returns a counter variable. - // Example: const newCounter = counter(); - // newCounter(); // 1 - // newCounter(); // 2 + // Return a function that when invoked increments and returns a counter + // variable. Example: const newCounter = counter(); newCounter(); // 1 + // + let privateCounter = 0; + return () => { + const increase = () => { + privateCounter++; + return privateCounter; + }; + return increase(); + }; }; 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 privateCounter = 0; + return { increment: () => { privateCounter++; return privateCounter; }, decrement: () => { privateCounter--; return privateCounter; } }; }; const limitFunctionCallCount = (cb, n) => { // Should return a function that invokes `cb`. // The returned function should only allow `cb` to be invoked `n` times. + let limiter = n; + // should create a function to return + return (...args) => { + if (limiter > 0) { + limiter--; + return cb.apply(cb, args); + } + return null; + }; }; const cacheFunction = (cb) => { @@ -25,6 +43,9 @@ 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. + return () => { + cb(); + }; }; /* eslint-enable no-unused-vars */ diff --git a/src/this.js b/src/this.js index 8ea3020..6244ba5 100644 --- a/src/this.js +++ b/src/this.js @@ -5,6 +5,11 @@ class User { constructor(options) { // set a username and password property on the user object that is created + this.password = options.password; + this.username = options.username; + } + checkPassword(passwordToCompare) { + return this.password === passwordToCompare; } // 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 @@ -15,6 +20,7 @@ const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatteryst const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` const checkPassword = function comparePasswords(passwordToCompare) { + return 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 @@ -25,7 +31,7 @@ const checkPassword = function comparePasswords(passwordToCompare) { // use .call, .apply, and .bind // .call - +//checkPassword.call() // .apply // .bind From 2cf93c14caba07de07767482a7e43f88c2e8b382 Mon Sep 17 00:00:00 2001 From: Allan Felipe Date: Thu, 13 Jul 2017 23:25:08 -0300 Subject: [PATCH 6/6] BTwo first functions for Objects --- src/objects.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/objects.js b/src/objects.js index ba39c6c..d21afe1 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,12 +5,17 @@ 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.keys(obj).map((key) => { + return obj[key]; + }); }; const mapObject = (obj, cb) => {