diff --git a/package-lock.json b/package-lock.json index c2d7130..baf523a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3945,6 +3945,11 @@ "dev": true, "optional": true }, + "underscore": { + "version": "1.8.3", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" + }, "user-home": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/user-home/-/user-home-2.0.0.tgz", diff --git a/package.json b/package.json index d23afbd..cecedb5 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "dependencies": { "babel-preset-es2015": "^6.24.0", - "eslint-config-airbnb": "^14.1.0" + "eslint-config-airbnb": "^14.1.0", + "underscore": "^1.8.3" } } diff --git a/src/arrays.js b/src/arrays.js index b995952..41f65f4 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -8,34 +8,62 @@ 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 newArr = []; + for (let i = 0; i < elements.length; i++) { newArr.push(cb(elements[i])); } + return newArr; }; 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. + // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value + if (memo === undefined) 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])) 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 newArr = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) newArr.push(elements[i]); + } + return newArr; }; /* Extra Credit */ 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 newArr = []; + for (let i = 0; i < elements.length; i++) { + if (elements[i].length) { + elements = elements.concat(elements[i]); + } else { + newArr.push(elements[i]); + } + } + return newArr; }; /* eslint-enable no-unused-vars, max-len */ diff --git a/src/class.js b/src/class.js index 8276e29..aaee2d7 100644 --- a/src/class.js +++ b/src/class.js @@ -19,6 +19,31 @@ // `meow` that should return the string ` meowed!` where `` is the `name` // property set on the Cat instance. +class User { + constructor(options) { + this.email = options.email; + this.password = options.password; + } + comparePasswords(pass) { + if (pass === this.password) return true; + return false; + } +} +class Animal { + constructor(options) { + this.age = options.age; + } + growOlder() { return ++this.age; } + +} +class Cat extends Animal { + constructor(options) { + super(options); + this.name = super.name; + } + meow() { return `${this.name} meowed!`; } +} + module.exports = { User, diff --git a/src/closure.js b/src/closure.js index 2d6592f..f57925c 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,17 +5,46 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + const inc = () => { + count++; + return count; + }; + return inc; +}; +// +const newObj = { + numUp: 0, + numDown: 0, + increment: function increment() { + this.numUp++; + return this.numUp; + }, + decrement: function decrement() { + this.numDown--; + return this.numDown; + } }; - 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. + return newObj; }; + const limitFunctionCallCount = (cb, n) => { // Should return a function that invokes `cb`. - // The returned function should only allow `cb` to be invoked `n` times. + // The returned function should only allow `cb` to be invoked `n` times + let count = 0; + return (...args) => { + if (count < n) { + count++; + } else { + return null; + } + return cb(...args); + }; }; /* Extra Credit */ @@ -26,6 +55,11 @@ 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 (arg) => { + if (arg in cache) return cache[arg]; + return cache[arg] = cb(arg); + }; }; /* eslint-enable no-unused-vars */ diff --git a/src/es6.js b/src/es6.js index eb846ab..baa93ff 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,30 +7,30 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +let 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) { +const User = (options) => { this.username = options.username; this.password = options.password; - this.sayHi = function() { + this.sayHi = () => { return this.username + ' says hello!'; }; } -var username = 'JavaScriptForever'; -var password = 'password'; +const username = 'JavaScriptForever'; +const password = 'password'; -var me = new User({ +const me = User({ username: username, password: password, }); @@ -38,19 +38,19 @@ var me = new User({ // ---------------- // let, const, =>, ... (spread operator) -var addArgs = function () { - var sum = 0; - for (var i = 0; i < arguments.length; i++) { - sum += arguments[i]; +const addArgs = (...arg) => { + let sum = 0; + for (let i = 0; i < arg.length; i++) { + sum += arg[i]; } return sum; }; -var argsToCb = function (cb) { - var args = Array.prototype.slice.call(arguments); +const argsToCb = (cb, ...arg) => { + const args = Array.prototype.slice.call(arg); return cb.apply(null, args.splice(1)); }; -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..2699b31 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,38 +1,57 @@ // 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. // http://underscorejs.org/#mapObject + for (let i = 0; i < keys(obj).length; i++) { + obj[keys(obj)[i]] = cb(values(obj)[i]); + } + return obj; }; - const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs + const outArr = []; + for (let i = 0; i < keys(obj).length; i++) { + const subArr = []; + subArr.push(keys(obj)[i], values(obj)[i]); + outArr[i] = subArr; + } + return outArr; }; 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 = {}; + for (let i = 0; i < keys(obj).length; i++) { + newObj[values(obj)[i]] = keys(obj)[i]; + } + return newObj; }; const defaults = (obj, defaultProps) => { // Fill in undefined properties that match properties on the `defaultProps` parameter object. // Return `obj`. // http://underscorejs.org/#defaults + for (let i = 0; i < keys(defaultProps).length; i++) { + if (obj[keys(defaultProps)[i]] === undefined) { + obj[keys(defaultProps)[i]] = values(defaultProps)[i]; + } + } + return obj; }; /* eslint-enable no-unused-vars */ diff --git a/src/recursion.js b/src/recursion.js index eb65c57..0c6627d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,17 +3,29 @@ 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 === 0) return 1; + return n * nFactorial(n - 1); }; - /* Extra Credit */ const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + const checkBox = []; + function checkNested(subObj) { + for (let i = 0; i < Object.keys(subObj).length; i++) { + if (typeof subObj[Object.keys(subObj)[i]] !== 'object') checkBox.push(subObj[Object.keys(subObj)[i]]); + if (typeof subObj[Object.keys(subObj)[i]] === 'object') checkNested(subObj[Object.keys(subObj)[i]]); + } + return checkBox.every(allkeys => allkeys === checkBox[0]); + } + return checkNested(obj); }; /* eslint-enable no-unused-vars */ diff --git a/src/this.js b/src/this.js index 8ea3020..7991a96 100644 --- a/src/this.js +++ b/src/this.js @@ -5,6 +5,9 @@ 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; + this.checkPassword = string => string === this.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 @@ -14,18 +17,17 @@ class User { const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` -const checkPassword = function comparePasswords(passwordToCompare) { +const checkPassword = passwordToCompare => 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 - // note that we use the `function` keyword and not `=>` -}; + // note that we use the `function` keyword and not `=>' // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind - // .call - +console.log(checkPassword.call(this, User.password)); // .apply - +console.log(checkPassword.apply(this, User.password)); // .bind +console.log(checkPassword.bind(this)());