diff --git a/src/arrays.js b/src/arrays.js index f24d6ef..624df6b 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,33 +6,102 @@ 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 resultArray = []; + // for (let i = 0; i < elements.length; i++) { + // resultArray.push(cb(elements[i])); + // THIS IS THE for loop version (above) + // + // THIS IS THE forEach version (below) + each(elements, (elem) => { + resultArray.push(cb(elem)); + }); + return resultArray; }; const reduce = (elements, cb, memo) => { // 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++){ + let i = 0; + if (memo === undefined) { + memo = elements[0]; + i = 1; + } + for (; 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]) === true) { + return elements[i]; + } + } }; 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 trueArray = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) { + trueArray.push(elements[i]); + } + } + return trueArray; }; const flatten = (elements) => { // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; + // +// elements = map(elements, (elem) => { +// if (Array.isArray(elem)) { +// return flatten(elem); +// } +// return elem; +// }); +// const result = []; +// each(elements, (elem) => { +// if (Array.isArray(elem)) { +// const nestedArray = elem; +// each(nestedArray, (nestedElem) => { +// result.push(nestedElem); +// }); +// } else { +// result.push(elem); +// } +// }); +// return result; +// }; +// or + let result = []; + each(elements, (elem) => { + if (Array.isArray(elem)) { + const nestedArray = flatten(elem); + result = result.concat(nestedArray); + } else { + result.push(elem); + } + }); + return result; }; /* eslint-enable no-unused-vars, max-len */ diff --git a/src/class.js b/src/class.js index 8276e29..37be978 100644 --- a/src/class.js +++ b/src/class.js @@ -6,6 +6,16 @@ // 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(pw) { + if (pw === this.password) return true; + return false; + } +} /* eslint-disable no-undef */ // Remove this comment once you write your classes. @@ -14,11 +24,29 @@ // `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. +// `growOlder` that returns the age. + a year. // 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..b7324f0 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,7 +5,13 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + return () => { + count++; + return count; + }; }; +const counterFunction = counter(); const counterFactory = () => { // Return an object that has two methods called `increment` and `decrement`. diff --git a/src/es6.js b/src/es6.js index eb846ab..33c1d05 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,7 +7,9 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +//var food = 'pineapple'; +// var changes to const +const food = 'pineapple'; var isMyFavoriteFood = function(food) { food = food || 'thousand-year-old egg'; //This sets a default value if `food` is falsey diff --git a/src/objects.js b/src/objects.js index ba39c6c..f564745 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,34 +5,65 @@ 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 + const vals = Object.keys(obj).map((key) => { + return obj[key]; + }); + return vals; + // return Object.values(obj); + // coming up not a function but clearly is and should pass }; const mapObject = (obj, cb) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject + const newObj = {}; + Object.keys(obj).forEach((key) => { + newObj[key] = cb(obj[key]); + }); + return newObj; }; const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. - // http://underscorejs.org/#pairs + // http://underscorejs.org/# + const pairArray = []; + Object.keys(obj).forEach((key) => { + const nestPairArr = []; + nestPairArr.push(key, obj[key]); + pairArray.push(nestPairArr); + }); + return pairArray; }; 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 = {}; + Object.keys(obj).forEach((key) => { + newObj[obj[key]] = key; + }); + return newObj; }; 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((key) => { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + return; + } + obj[key] = defaultProps[key]; + }); + return obj; }; /* eslint-enable no-unused-vars */