From 1d04a75a288c74cf7b4303d71377d65e8a41cbf4 Mon Sep 17 00:00:00 2001 From: Christopher Atoki Date: Mon, 11 Sep 2017 11:55:56 -0400 Subject: [PATCH 1/3] Sumbitted Advanced JS Project --- .DS_Store | Bin 0 -> 8196 bytes package.json | 1 + src/.DS_Store | Bin 0 -> 6148 bytes src/arrays.js | 24 +++++++++++++++++- src/class.js | 28 ++++++++++++++++++++- src/closure.js | 8 ++++++ src/es6.js | 65 ++++++++++++++++++++++++------------------------- src/objects.js | 38 +++++++++++++++++++++++++++++ src/this.js | 13 ++++++++++ 9 files changed, 142 insertions(+), 35 deletions(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..388963e220d72d447153c795cd2f1ed3d766ec11 GIT binary patch literal 8196 zcmeHM-HOvd6h5=_|h>AqypeWqD1h>T0 zc~ur{&9-a+1^7g5Iw45`4JcZO<_1=P6<`He0akz&I138kovqAS@!mIQ*|P$yz<;TL zdOo-)3hNdp2KA!@PNx9CJeswkjyynfT#I#!6N8$HYntpqSgNoohA?!TcU3sBZgFDJ z&`B6N3EQ)<8H&)ogXijS5_N;@SpimHQ2~{^Z&0kW_HZtL4=LG?WXF%Bj~KPA?xNiI zp$)ZDc>XZyS>9K_7j=`gT>d83a{2WO7o9@EDO`45wvObmm3rwQX?XqT`luU(zO6rz zGMd&s|7kZIwl}Xmka6mDpb$(BXD&`?akt7RIBcm+*`ZV zamgLkYUPq!t=t)p9cSzM&b|GElQ(Zi@5b*x>LHP^&lkF4j;H?9X#;-U>c^3c4-upI zSE)xHb?6oRA%%1k+aXdPZ__a)sK*&;zkoF_?fey1@WfVzSD?qVNB5~o4}sg*jtS;_ z5+5>*9PX!2olMmCeE0t%q8qEJxh0>mZ~Ad8hSDd>C(4!|j>xDyp6yqQ^y z9Xl3&lxx^n0O` zOBJm&1x$gq0z2BA@cDna`TM^eWGz#`6!=#PxWQ~b iMUG-{`6xbwi=p3h1(-&x6ww2-7XgvM3RB=u75E0xt8E(q literal 0 HcmV?d00001 diff --git a/src/arrays.js b/src/arrays.js index b995952..18bf8b7 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -8,17 +8,29 @@ 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], i, elements)); + } + 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. + for (let i = 0; i < elements.length; i++) { + memo = cb(memo, elements[i]); + } + return memo; }; const find = (elements, cb) => { @@ -36,8 +48,18 @@ 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 flat = []; + for (let i = 0; i < elements.length; i++) { + // if i !== array then push to flat + // else if i === array call flatten and concat to flatten + if (Array.isArray(elements[i])) { + flat = flat.concat(flatten(elements[i])); + } else { + flat.push(elements[i]); + } + } + return flat; }; - /* eslint-enable no-unused-vars, max-len */ module.exports = { diff --git a/src/class.js b/src/class.js index 8276e29..020d8be 100644 --- a/src/class.js +++ b/src/class.js @@ -1,16 +1,42 @@ // Create a class called User. +class User { // The constructor of the class should have a parameter called `options`. + constructor(options) { // `options` will be an object that will have the properties `email` and `password`. // Set the `email` and `password` properties on the class. + this.email = options.email; + this.password = options.password; // Add a method called `comparePasswords`. `comparePasswords` should have a parameter + this.comparePasswords = (potentialPassword) => { // 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. - + if (potentialPassword === this.password) { + return true; + } return false; + }; + } +} /* eslint-disable no-undef */ // Remove this comment once you write your classes. // Create a class called `Animal` and a class called `Cat`. +class Animal { +// Animal and Cat should both have a parameter called `options` in their constructors. + constructor(options) { + this.age = options.age; + this.name = options.name; + this.growOlder = (years) => { + return this.age + 1; + }; + } +} + +class Cat extends Animal { + meow() { + return `${this.name} meowed!`; + } +} // `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 diff --git a/src/closure.js b/src/closure.js index 2d6592f..3db6b33 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,6 +5,14 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + // create a point for the counter to start at + let num = 0; + // create a function which increase num by one + const addOne = () => { + return num += 1; + } + // return the addOne to show the new value of num + return addOne(); }; const counterFactory = () => { diff --git a/src/es6.js b/src/es6.js index eb846ab..9393b3c 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,50 +7,49 @@ //---------------- // 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); +let 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, -}); +const User = function (options) { + this.username = options.username; + this.password = options.password; + this.sayHi = () => { + 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 - +const addArgs = () => { + let sum = 0; + for (let i = 0; i < arguments.length; i++) { + sum += arguments[i]; + } + return sum; + }; + + let argsToCb = (cb) => { + let args = Array.prototype.slice.call(arguments); + return cb.apply(null, args.splice(1)); + }; + + let 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..5ffccc7 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,34 +5,72 @@ 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.getOwnPropertyNames(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 + // Get the values from the obj + const arrValues = Object.values(obj); + const arrKeys = Object.keys(obj); + let newValue = ''; + // Loop thru the values + for (let i = 0; i < arrValues.length; i++) { + // send them each into the callback function to get new values + newValue = cb(arrValues[i]); + // add new values to the keys of the obj + obj[arrKeys[i]] = newValue; + } + return obj; }; 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 arrKeys = Object.keys(obj); + const arrValues = Object.values(obj); + const newObj = {}; + for (let i = 0; i < arrValues.length; i++) { + newObj[arrValues[i]] = arrKeys[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 + + // get the keys for obj + const arrKeys = Object.keys(obj); + // get the entries for defaultProps + const arrDefaultKeys = Object.keys(defaultProps); + const arrDefaultValues = Object.values(defaultProps); + // loop thru the keys for arrDefaultKeys and check to see if they are in arrKeys + for (let i = 0; i < arrDefaultKeys.length; i++) { + if (arrKeys.includes(arrDefaultKeys[i])) { + // That's good. Move on to the next one + } else { + // Since it's not included, add it to obj as a new property and values + obj[arrDefaultKeys[i]] = arrDefaultValues[i]; + } + } + return obj; }; /* eslint-enable no-unused-vars */ diff --git a/src/this.js b/src/this.js index 8ea3020..82c7475 100644 --- a/src/this.js +++ b/src/this.js @@ -5,6 +5,13 @@ 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) => { + if (string === this.password) { + return true; + } return false; + }; } // 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 @@ -19,13 +26,19 @@ 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 // .call +me.checkPassword.call(me); // .apply +me.checkPassword.apply(me); // .bind +me.checkPassword.bind(me)(); From c41cb7249f6eb7cbf1ae6f8ba650e90e9efbfc72 Mon Sep 17 00:00:00 2001 From: Christopher Atoki Date: Mon, 11 Sep 2017 12:17:19 -0400 Subject: [PATCH 2/3] Advanced JS Project --- src/arrays.js | 2 +- src/class.js | 2 +- src/closure.js | 1 - src/es6.js | 2 +- src/objects.js | 2 +- src/this.js | 2 +- 6 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 18bf8b7..334fb19 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -5,7 +5,7 @@ // 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. + // 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++) { diff --git a/src/class.js b/src/class.js index 020d8be..9f1cea5 100644 --- a/src/class.js +++ b/src/class.js @@ -9,7 +9,7 @@ class User { // Add a method called `comparePasswords`. `comparePasswords` should have a parameter this.comparePasswords = (potentialPassword) => { // 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. +// Return true if the potential password matches the `password` property. Otherwise return false. if (potentialPassword === this.password) { return true; } return false; diff --git a/src/closure.js b/src/closure.js index 3db6b33..57de862 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,7 +5,6 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 - // create a point for the counter to start at let num = 0; // create a function which increase num by one const addOne = () => { diff --git a/src/es6.js b/src/es6.js index 9393b3c..da4d1a4 100644 --- a/src/es6.js +++ b/src/es6.js @@ -10,7 +10,7 @@ let food = 'pineapple'; const isMyFavoriteFood = (food) => { - food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey + food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey return food === 'thousand-year-old egg'; }; diff --git a/src/objects.js b/src/objects.js index 5ffccc7..e542574 100644 --- a/src/objects.js +++ b/src/objects.js @@ -16,7 +16,7 @@ const values = (obj) => { }; const mapObject = (obj, cb) => { - // Like map for arrays, but for objects. Transform the value of each property in turn. + // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject // Get the values from the obj const arrValues = Object.values(obj); diff --git a/src/this.js b/src/this.js index 82c7475..f70fd80 100644 --- a/src/this.js +++ b/src/this.js @@ -15,7 +15,7 @@ class User { } // 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` + // return `true` if they match, otherwise return `false` } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); From 9078bcbe0782a81b9b7124d906809e3f0e9f2c06 Mon Sep 17 00:00:00 2001 From: Christopher Atoki Date: Mon, 11 Sep 2017 12:24:40 -0400 Subject: [PATCH 3/3] Advanced JS Project --- README.md | 15 +++++++-------- src/arrays.js | 2 +- src/class.js | 2 +- src/closure.js | 2 +- src/es6.js | 2 +- src/objects.js | 2 +- src/this.js | 2 +- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 958568d..beeeec4 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ # Advanced JavaScript ## Instructions - -Fork and clone this repo. - - * Run the command `npm i` to install needed node packages. - * Run the command `npm test` to run the tests. - * Work through the files and make the tests pass. - * Suggested order: `es6.js`, `arrays.js`, `objects.js`, `this.js`, `class.js`, `closure.js`, and then `recursion.js`. - * Submit a pull request when you are finished and we will review your code. +## Do not use any of the built in Javascript Array methods like .forEach .map etc. If you'd like feel free to reuse any of your functions along the way though! +#### Fork and clone this repo. +* Run the command `npm i` to install needed node packages. +* Run the command `npm test` to run the tests. +* Work through the files and make the tests pass. +* Suggested order: `es6.js`, `arrays.js`, `objects.js`, `this.js`, `class.js`, `closure.js`, and then `recursion.js`. +* Submit a pull request when you are finished and we will review your code. diff --git a/src/arrays.js b/src/arrays.js index 334fb19..18bf8b7 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -5,7 +5,7 @@ // 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. + // 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++) { diff --git a/src/class.js b/src/class.js index 9f1cea5..d66a547 100644 --- a/src/class.js +++ b/src/class.js @@ -17,7 +17,7 @@ class User { } } -/* eslint-disable no-undef */ // Remove this comment once you write your classes. +/* eslint-disable no-undef */ // Remove this comment once you write your classes. // Create a class called `Animal` and a class called `Cat`. diff --git a/src/closure.js b/src/closure.js index 57de862..bed5263 100644 --- a/src/closure.js +++ b/src/closure.js @@ -16,7 +16,7 @@ const counter = () => { 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. + // `increment` should increment a counter variable in closure scope and return it. // `decrement` should decrement the counter variable and return it. }; diff --git a/src/es6.js b/src/es6.js index da4d1a4..9393b3c 100644 --- a/src/es6.js +++ b/src/es6.js @@ -10,7 +10,7 @@ let food = 'pineapple'; const isMyFavoriteFood = (food) => { - food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey + food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey return food === 'thousand-year-old egg'; }; diff --git a/src/objects.js b/src/objects.js index e542574..5ffccc7 100644 --- a/src/objects.js +++ b/src/objects.js @@ -16,7 +16,7 @@ const values = (obj) => { }; const mapObject = (obj, cb) => { - // Like map for arrays, but for objects. Transform the value of each property in turn. + // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject // Get the values from the obj const arrValues = Object.values(obj); diff --git a/src/this.js b/src/this.js index f70fd80..fd64141 100644 --- a/src/this.js +++ b/src/this.js @@ -4,7 +4,7 @@ class User { constructor(options) { - // set a username and password property on the user object that is created + // set a username and password property on the user object that is created this.username = options.username; this.password = options.password; this.checkPassword = (string) => {