From 246d66c335a8c9ae30a68e232b14739a06b0c48f Mon Sep 17 00:00:00 2001 From: Keagan Goetsch Date: Sun, 10 Sep 2017 09:51:39 -0500 Subject: [PATCH 1/7] Add files via upload array.js homework complete --- src/arrays.js | 137 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 50 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index b995952..9bb8bee 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,50 +1,87 @@ -// Complete the following functions. -// These functions only need to work with arrays. -// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc. -// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating -// You can use the functions that you have already written to help solve the other problems - -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 -}; - -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 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. -}; - -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. -}; - -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 -}; - -/* 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]; -}; - -/* eslint-enable no-unused-vars, max-len */ - -module.exports = { - each, - map, - reduce, - find, - filter, - flatten -}; +// Complete the following functions. +// These functions only need to work with arrays. +// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc. +// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating +// You can use the functions that you have already written to help solve the other problems + +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 newArray = []; + for (let i = 0; i < elements.length; i++) { + newArray.push(cb(elements[i])); + } + return newArray; + +}; + +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 = memo; + for (let i = 0; i < elements.length; i++) { + result = cb(reduced, elements[i]); + } + return reduced; +}; + +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 truthy; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) { + truthy = elements[i]; + } + } + return truthy; +}; + +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 truthyArr = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) { + truthyArr.push(elements[i]); + } + } + return truthyArr; +}; + +/* 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 result = []; + for(let i = 0; i < elements.length; i++) { + if(Array.isArray(elements[i])) { + result = result.concat(flatten(elements[i])); + } else { + result.push(elements[i]); + } + } + return result; +}; + +/* eslint-enable no-unused-vars, max-len */ + +module.exports = { + each, + map, + reduce, + find, + filter, + flatten +}; From 71cc59f1088deb8d8559e815570ce24acb3b1c86 Mon Sep 17 00:00:00 2001 From: Keagan Goetsch Date: Sun, 10 Sep 2017 09:58:02 -0500 Subject: [PATCH 2/7] classes created class.js homework complete --- src/class.js | 78 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/src/class.js b/src/class.js index 8276e29..cae7ab8 100644 --- a/src/class.js +++ b/src/class.js @@ -1,26 +1,52 @@ -// Create a class called User. -// The constructor of the class should have a parameter called `options`. -// `options` will be an object that will have the properties `email` and `password`. -// Set the `email` and `password` properties on the class. -// 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. - - -// 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. -// Animal should have the property `age` that's set in the constructor and the method -// `growOlder` that returns the age. -// 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. - - -module.exports = { - User, - Cat -}; +// Create a class called User. +// The constructor of the class should have a parameter called `options`. +// `options` will be an object that will have the properties `email` and `password`. +// Set the `email` and `password` properties on the class. +// 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(password) { + return this.password === password; + } +} + +/* eslint-disable no-undef */ // Remove this comment once you write your classes. + + +// 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. +// Animal should have the property `age` that's set in the constructor and the method +// `growOlder` that returns the age. +// 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() { + this.age += 1; + return this.age; + } +} + +class Cat extends Animal { + constructor(options) { + super(options); + this.name = options.name; + } + meow() { + return `${this.name} meowed!`; + } +} + +module.exports = { + User, + Cat +}; From 0e12e3d412e75eb09c4a760e4b24cd1a5475fce3 Mon Sep 17 00:00:00 2001 From: Keagan Goetsch Date: Sun, 10 Sep 2017 10:02:40 -0500 Subject: [PATCH 3/7] closure homework complete --- src/closure.js | 100 ++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 38 deletions(-) diff --git a/src/closure.js b/src/closure.js index 2d6592f..4bbb5dc 100644 --- a/src/closure.js +++ b/src/closure.js @@ -1,38 +1,62 @@ -// 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 -}; - -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. -}; - -const limitFunctionCallCount = (cb, n) => { - // Should return a function that invokes `cb`. - // The returned function should only allow `cb` to be invoked `n` times. -}; - -/* Extra Credit */ -const cacheFunction = (cb) => { - // Should return a funciton that invokes `cb`. - // A cache (object) should be kept in closure scope. - // The cache should keep track of all arguments have been used to invoke this function. - // 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. -}; - -/* eslint-enable no-unused-vars */ - -module.exports = { - counter, - counterFactory, - cacheFunction, - limitFunctionCallCount -}; +// 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 + let count = 0; + return () => count += 1; +}; + +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: () => count += 1, + decrement: () => count -= 1 + }; +}; + +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) { + return null; + } + count += 1; + return cb(...args); + }; +}; + +/* Extra Credit */ +const cacheFunction = (cb) => { + // Should return a funciton that invokes `cb`. + // A cache (object) should be kept in closure scope. + // The cache should keep track of all arguments have been used to invoke this function. + // 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 storage = {}; + + return (input) => { + if (Object.prototype.hasOwnProperty.call(storage, input)) { + return storage[input]; + } + storage[input] = cb(input); + return storage[input]; + }; +}; + +/* eslint-enable no-unused-vars */ + +module.exports = { + counter, + counterFactory, + cacheFunction, + limitFunctionCallCount +}; From 81c6a2a6223252313c6f9dc3857ed890c4a0ff41 Mon Sep 17 00:00:00 2001 From: Keagan Goetsch Date: Sun, 10 Sep 2017 10:03:38 -0500 Subject: [PATCH 4/7] es6 easy peasy --- src/es6.js | 112 ++++++++++++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/src/es6.js b/src/es6.js index eb846ab..8f3f9ba 100644 --- a/src/es6.js +++ b/src/es6.js @@ -1,56 +1,56 @@ -/* eslint-disable */ - -// Refactor the following code to use the specified ES6 features. -// There are no automated tests. -// To make sure the code still works you can run this file using `node es6.js` from inside `/src`. - -//---------------- -// const, =>, default parameters, arrow functions default return statements using () - -var 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'; -}; - -var 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!'; - }; -} - -var username = 'JavaScriptForever'; -var password = 'password'; - -var me = new User({ - username: username, - password: password, -}); - -// ---------------- -// let, const, =>, ... (spread operator) - -var addArgs = function () { - var sum = 0; - for (var 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)); -}; - -var result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 - -/* eslint-enable */ +/* eslint-disable */ + +// Refactor the following code to use the specified ES6 features. +// There are no automated tests. +// To make sure the code still works you can run this file using `node es6.js` from inside `/src`. + +//---------------- +// const, =>, default parameters, arrow functions default return statements using () + +const food = 'pineapple'; + +const isMyFavoriteFood = food => { + food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey + return food === 'thousand-year-old egg'; +}; + +const isThisMyFavorite = isMyFavoriteFood(food); + +//---------------- +//const, class, template literals, enhanced object literals (foo: foo, -> foo,) + +const User = function(options) { + this.username = options.username; + this.password = options.password; + this.sayHi = function() { + return `${this.username} says hello!`; + }; +}; + +const username = 'JavaScriptForever'; +const password = 'password'; + +const me = new User({ + username, + password, +}); + +// ---------------- +// let, const, =>, ... (spread operator) + +const addArgs = function () { + let sum = 0; + for (let i = 0; i < arguments.length; i++) { + sum += arguments[i]; + } + return sum; +}; + +const argsToCb = function (cb) { + const args = Array.prototype.slice.call(arguments); + return cb(...args.splice(1)); +}; + +const result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 + +/* eslint-enable */ From c9152c89f11a56bc0131aa2240c033609bf74949 Mon Sep 17 00:00:00 2001 From: Keagan Goetsch Date: Sun, 10 Sep 2017 10:12:02 -0500 Subject: [PATCH 5/7] objects created --- src/objects.js | 116 +++++++++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 47 deletions(-) diff --git a/src/objects.js b/src/objects.js index ba39c6c..e789529 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,47 +1,69 @@ -// Complete the following underscore functions. -// Reference http://underscorejs.org/ for examples. - -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 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 -}; - -const pairs = (obj) => { - // Convert an object into a list of [key, value] pairs. - // http://underscorejs.org/#pairs -}; - -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 defaults = (obj, defaultProps) => { - // Fill in undefined properties that match properties on the `defaultProps` parameter object. - // Return `obj`. - // http://underscorejs.org/#defaults -}; - -/* eslint-enable no-unused-vars */ - -module.exports = { - keys, - values, - mapObject, - pairs, - invert, - defaults -}; +// Complete the following underscore functions. +// Reference http://underscorejs.org/ for examples. + +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 + const keyArr = Object.keys(obj); + const result = {}; + + keyArr.forEach((k) => { + result[k] = cb(obj[k]); + }); + return result; +}; + +const pairs = (obj) => { + // Convert an object into a list of [key, value] pairs. + // http://underscorejs.org/#pairs + return Object.entries(obj); +}; + +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 keyArr = Object.keys(obj); + const result = {}; + + keyArr.forEach(k => result[obj[k]] = k); + return result; +}; + +const defaults = (obj, defaultProps) => { + // Fill in undefined properties that match properties on the `defaultProps` parameter object. + // Return `obj`. + // http://underscorejs.org/#defaults + const keyArr = Object.keys(defaultProps); + keyArr.forEach((k) => { + if (!obj[k]) { + obj[k] = defaultProps[k]; + } + }); + return obj; +}; + +/* eslint-enable no-unused-vars */ + +module.exports = { + keys, + values, + mapObject, + pairs, + invert, + defaults +}; From 5136e02710ca5fa13783c2c496d38a0e714c07a1 Mon Sep 17 00:00:00 2001 From: Keagan Goetsch Date: Sun, 10 Sep 2017 10:24:23 -0500 Subject: [PATCH 6/7] recursion homework done --- src/recursion.js | 87 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 25 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index eb65c57..1117b00 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,25 +1,62 @@ -// Complete the following functions. - -const nFibonacci = (n) => { - // fibonacci sequence: 1 2 3 5 8 13 ... - // return the nth number in the sequence -}; - -const nFactorial = (n) => { - // factorial example: !5 = 5 * 4 * 3 * 2 * 1 - // return the factorial of `n` -}; - -/* Extra Credit */ -const checkMatchingLeaves = (obj) => { - // return true if every property on `obj` is the same - // otherwise return false -}; - -/* eslint-enable no-unused-vars */ - -module.exports = { - nFibonacci, - nFactorial, - checkMatchingLeaves -}; +// Complete the following functions. + +const nFibonacci = (n) => { + // fibonacci sequence: 1 2 3 5 8 13 ... + // return the nth number in the sequence + if (n===1) + { + return [0, 1]; + } + else + { + const s = nFibonacci(n - 1); + s.push(s[s.length - 1] + s[s.length - 2]); + return s; + } +}; + +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); +}; +console.log(nFactorial(10)); + +/* Extra Credit */ +const checkMatchingLeaves = (obj) => { + // return true if every property on `obj` is the same + // otherwise return false + let value; + let allMatch = true; + + const checkLeaves = (object) => { + Object.keys(object).forEach((key) => { + if (value === undefined && typeof key !== 'object') { + value = object[key]; + return undefined; + } + if (typeof object[key] === 'object') { + return checkLeaves(object[key]); + } + if (object[key] !== value) { + allMatch = false; + return undefined; + } + return undefined; + }); + }; + checkLeaves(obj); + return allMatch; +}; +console.log(checkMatchingLeaves(myObj1 = {title:true, name:true, email:false})) +/* eslint-enable no-unused-vars */ + +module.exports = { + nFibonacci, + nFactorial, + checkMatchingLeaves +}; From 7214c92840cbbff55498675c8a2759122cba612e Mon Sep 17 00:00:00 2001 From: Keagan Goetsch Date: Sun, 10 Sep 2017 10:45:57 -0500 Subject: [PATCH 7/7] this homework done --- src/this.js | 72 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/src/this.js b/src/this.js index 8ea3020..942196f 100644 --- a/src/this.js +++ b/src/this.js @@ -1,31 +1,41 @@ -// Follow the instructions and fill in the blank sections. -// There are no tests for this file. -// To verify your code works you can run this file using `node this.js` while in the `/src` folder - -class User { - constructor(options) { - // set a username and password property on the user object that is created - } - // 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` -} - -const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); -const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` - -const checkPassword = function comparePasswords(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 `=>` -}; - -// invoke `checkPassword` on `me` by explicitly setting the `this` context -// use .call, .apply, and .bind - -// .call - -// .apply - -// .bind +// Follow the instructions and fill in the blank sections. +// There are no tests for this file. +// To verify your code works you can run this file using `node this.js` while in the `/src` folder + +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(password) { + return this.password === password; + } +} + +const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); +const result = me.checkPassword('correcthorsebatterystaple'); // should return `true` +console.log(result); + +const checkPassword = function comparePasswords(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 `=>` + return this.password === passwordToCompare; +}; + +console.log(checkPassword("blorp")); +// invoke `checkPassword` on `me` by explicitly setting the `this` context + // use .call, .apply, and .bind + + // .call + checkPassword.call(me, 'correcthorsebatterystaple'); + + // .apply + checkPassword.apply(me, ['correcthorsebatterystaple']); + // .bind + checkPassword.bind(me, 'correcthorsebatterystaple')(); \ No newline at end of file