diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..388963e Binary files /dev/null and b/.DS_Store differ diff --git a/package.json b/package.json index d23afbd..a69d3da 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "regenerator-runtime": "^0.10.3" }, "dependencies": { + "babel-polyfill": "^6.26.0", "babel-preset-es2015": "^6.24.0", "eslint-config-airbnb": "^14.1.0" } diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..daa70a2 Binary files /dev/null and b/src/.DS_Store differ 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..d66a547 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. +// 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. +/* 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..bed5263 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,11 +5,18 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + 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 = () => { // 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 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..fd64141 100644 --- a/src/this.js +++ b/src/this.js @@ -4,11 +4,18 @@ 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) => { + 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 - // return `true` if they match, otherwise return `false` + // return `true` if they match, otherwise return `false` } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); @@ -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)();