diff --git a/src/arrays.js b/src/arrays.js index f24d6ef..3b372ee 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,33 +6,87 @@ 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 + if (Array.isArray(elements)) { + for (let i = 0; i < elements.length; i++) { + cb(elements[i], i); + } + } else { + throw new Error(); + } + elements.forEach((item, index) => { + cb(item, index); + }); }; 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 myArray = []; + each(elements, (element, index) => { + myArray.push(cb(element, index)); + }); + // const myArray = elements.map((item, index) => { + // return cb(item, index); + // }); + + return myArray; }; 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. + + // let sum; + // if (memo === undefined) sum = elements[0]; + // memo.reduce((sum, elements) => { + // return cb(elements); + // }); + + // if (memo === undefined) { + // memo = elements[0]; + // } + // for (let i = 0; i < memo.length; i++) { + // memo.push.cb(elements); + // } + if (!memo) { + memo = elements.shift(); + } + 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]) === true) 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 arr = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) arr.push(elements[i]); + } + return arr; +}; +// flatten rearranges a nested array 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 arr = []; + for (let i = 0; i < elements.length; i++) { + if (Array.isArray(elements[i])) { + arr = arr.concat(flatten(elements[i])); + } else arr.push(elements[i]); + } return arr; }; /* eslint-enable no-unused-vars, max-len */ diff --git a/src/class.js b/src/class.js index 8276e29..414acd1 100644 --- a/src/class.js +++ b/src/class.js @@ -5,9 +5,20 @@ // 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(myPassword) { + if (this.password === myPassword) return true; + return false; + } +} -/* eslint-disable no-undef */ // Remove this comment once you write your classes. + + // Remove this comment once you write your classes. // Create a class called `Animal` and a class called `Cat`. @@ -19,6 +30,27 @@ // `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..0c8b0ed 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,17 +5,35 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + return () => ++count; }; 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: () => { + return ++count; + }, + decrement: () => { + return --count; + } + }; }; 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 (x) => { + count++; + if (n >= count) return cb(x); + return null; + }; }; const cacheFunction = (cb) => { @@ -25,6 +43,8 @@ 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 = {}; }; /* eslint-enable no-unused-vars */ diff --git a/src/es6.js b/src/es6.js index eb846ab..9fbba36 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,50 +7,53 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +let food = 'pineapple'; // false -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) { + +class User { + constructor(options) { this.username = options.username; this.password = options.password; - this.sayHi = function() { - return this.username + ' says hello!'; - }; } + sayHi() { + return `${this.username} says hello!`; + }; +} -var username = 'JavaScriptForever'; -var password = 'password'; +let username = 'JavaScriptForever'; +let password = 'password'; -var me = new User({ - username: username, - password: password, +const me = new User({ + username, + password }); // ---------------- // let, const, =>, ... (spread operator) -var addArgs = function () { - var sum = 0; - for (var i = 0; i < arguments.length; i++) { +const addArgs = () => { + let sum = 0; + for (let 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)); +const argsToCb = (cb) => { + let args = Array.prototype.slice.call(arguments); + return cb(...args); }; -var result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 +let result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 /* eslint-enable */ diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..e69de29 diff --git a/src/objects.js b/src/objects.js index ba39c6c..c307338 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,21 +1,50 @@ // Complete the following underscore functions. // Reference http://underscorejs.org/ for examples. +// Object.keys(obj).forEach((key) => {}); +// const testObj = { +// name: 'steve', +// age: 12, +// food: 'pizza' +// }; 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 keysString = []; + Object.keys(obj).forEach((key) => { + keysString.push(key); + }); + return keysString; }; +// console.log(keys(testObj)); + const values = (obj) => { // Return all of the values of the object's own properties. // Ignore functions // http://underscorejs.org/#values + // QUESTION: return Object.keys(obj).forEach(obj[keys]); + const keysString = []; + Object.keys(obj).forEach((key) => { + keysString.push(obj[key]); + }); + return keysString; }; const mapObject = (obj, cb) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject + // Loop over the elements and call them back + // to the callback then return the elements in a new array. + // const newArr = obj.forEach(cb() => { + // cb(obj); + // }); + // return + // const arrKeys = keys(obj); + // const arrValues = values(obj); + // for (let i = 0; i < arrValues.length; i++) { + // } }; const pairs = (obj) => { diff --git a/src/recursion.js b/src/recursion.js index a3e997e..307f886 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,16 +3,22 @@ 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 < 2) return 1; + return n * (nFactorial(n - 1)); }; - +// checkMatchingLeaves is very similar to 'contains' method in the tree data structure. const checkMatchingLeaves = (obj) => { // return true if every property on `obj` is the same // otherwise return false + + }; /* eslint-enable no-unused-vars */ diff --git a/src/scripts.js b/src/scripts.js new file mode 100644 index 0000000..e69de29 diff --git a/src/styles.css b/src/styles.css new file mode 100644 index 0000000..e69de29 diff --git a/src/this.js b/src/this.js index 8ea3020..4d32e00 100644 --- a/src/this.js +++ b/src/this.js @@ -5,10 +5,18 @@ 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(string) { + if (string === this.password) { + return true; + } return false; + } } const me = new User({ username: 'LambdaSchool', password: 'correcthorsebatterystaple' }); @@ -19,6 +27,9 @@ 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