diff --git a/src/arrays.js b/src/arrays.js index 26aaed3..75dd384 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -8,26 +8,47 @@ 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. + let result = memo; + for (let i = 0; i < elements.length; i++) { + result = cb(result, elements[i]); + } + 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 }; @@ -35,6 +56,12 @@ 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++) { + const roller = Array.isArray(elements[i]) ? flatten(elements[i]) : [elements[i]]; + array = array.concat(roller); + } + return array; }; /* eslint-enable no-unused-vars, max-len */ diff --git a/src/class.js b/src/class.js index 8276e29..aee07ae 100644 --- a/src/class.js +++ b/src/class.js @@ -5,9 +5,16 @@ // 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. - - -/* eslint-disable no-undef */ // Remove this comment once you write your classes. +class User { + constructor(options) { + this.email = options.email; + this.password = options.password; + } + comparePasswords(str) { + if (str === this.password) return true; + return false; + } +} // Create a class called `Animal` and a class called `Cat`. @@ -18,7 +25,20 @@ // 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/closure.js b/src/closure.js index 4c98af0..16df815 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,17 +5,29 @@ 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 (...args) => { + if (count < n) { count++; return cb(...args); } + return null; + }; }; const cacheFunction = (cb) => { @@ -25,8 +37,13 @@ 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)) return cache; + cache.push(...args); + return cb(...args); + }; }; - /* eslint-enable no-unused-vars */ module.exports = { diff --git a/src/es6.js b/src/es6.js index eb846ab..9c8aa07 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,50 +7,40 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +const food = 'pineapple'; -var isMyFavoriteFood = function(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 -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; + } + 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++) { - sum += arguments[i]; - } - return sum; -}; +const addArgs = (...args) => (args.reduce((memo, val) => (memo + val))); -var argsToCb = function (cb) { - var args = Array.prototype.slice.call(arguments); - return cb.apply(null, args.splice(1)); -}; +const argsToCb = (cb, ...args) => (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..9319f96 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,34 +5,46 @@ 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) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject + 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 array = []; + keys(obj).forEach(k => array.push([k, obj[k]])); + 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 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 + Object.keys(defaultProps).forEach((d) => { if (!obj[d]) obj[d] = defaultProps[d]; }); + return obj; }; /* eslint-enable no-unused-vars */ diff --git a/src/recursion.js b/src/recursion.js index a3e997e..50ffd7d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,16 +3,26 @@ const nFibonacci = (n) => { // 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` + 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 array = []; + const rf = (...args) => { + Object.values(...args).forEach((v) => { + return typeof v === 'object' ? rf(v) : array.push(v); + }); + }; + rf(obj); + return new Set(array).size === 1; }; /* eslint-enable no-unused-vars */ diff --git a/src/this.js b/src/this.js index 8ea3020..1023b5b 100644 --- a/src/this.js +++ b/src/this.js @@ -5,10 +5,16 @@ 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(str) { + if (str === this.password) return true; + return false; + } } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); @@ -19,13 +25,16 @@ 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 // use .call, .apply, and .bind - +checkPassword.call(me, this.password); // .call - +checkPassword.apply(me, [this.password]); // .apply - +const checkMyPassword = checkPassword.bind(me); +checkMyPassword(this.password); // .bind