diff --git a/src/arrays.js b/src/arrays.js index f24d6ef..71b450d 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,35 +6,111 @@ 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. + + // The transformedArray is going to hold the values that were transformed from cb function? + const transformedArray = []; + + for (let i = 0; i < elements.length; i++) { + // The ith place in the empty array is assigned the result of cb + transformedArray[i] = cb(elements[i]); + } + return transformedArray; }; 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. + + // basically sum an array + if (memo === undefined) { + memo = elements[0]; + for (let i = 1; i < elements.length; i++) { + memo = cb(memo, elements[i]); + // cb(elements[i]); + } + } else { + for (let i = 0; i < elements.length; i++) { + memo = cb(memo, elements[i]); + // cb(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++) { + // passes elements to cb and at the same time checks for bool of true, ignores false + if (cb(elements[i]) === true) { + return elements[i]; + // linter wouldn't let me use break + } // if no trues are found returns undefined. + // 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 + // Return an empty array if no elements pass the truth + + const truthArray = []; + + for (let i = 0; i < elements.length; i++) { + // passes elements to cb and at the same time checks for bool of true, ignores false + if (cb(elements[i]) === true) { + truthArray.push(elements[i]); + } + } + // want it outside the for loop so it can iterate and collect all trues + // and if no trues a re present then it returns empty + return truthArray; }; 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 flattenedArray = []; +// got it working for a normal nested array so commented out +// it passed the first test + // for (let i = 0; i < elements.length; i++) { + // for (let j = 0; j < elements.length; j++) { + // flattenedArray.push(elements[i][j]); + // } + // } + // return flattenedArray; + + for (let i = 0; i < elements.length; i++) { + // Need to check if the content (elements inside the current array) + // is another array. to do that use Array.isArray + // Array.isArray checks truth value to see if its an array + if (Array.isArray(elements[i])) { + // if an element in elements is an array keep going deeper until false + // This if calls the flatten(itself/recursion) function and check for true/false if element is an array + // if it is an array it concatinates it to flattenedArray, which makes a level of arrayness + // disappear and keeps getting called until it is false which means no array detected + flattenedArray = flattenedArray.concat(flatten(elements[i])); + } else { + flattenedArray.push(elements[i]); + } + } + return flattenedArray; }; + /* eslint-enable no-unused-vars, max-len */ module.exports = { diff --git a/src/class.js b/src/class.js index 8276e29..54e2775 100644 --- a/src/class.js +++ b/src/class.js @@ -6,9 +6,17 @@ // 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. - +// used https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes +class User { + constructor(options) { + this.email = options.email; + this.password = options.password; + } + comparePasswords(password) { + if (password === this.password) { return true; } + return false; + } +} // Create a class called `Animal` and a class called `Cat`. // `Cat` should extend the `Animal` class. @@ -19,6 +27,32 @@ // `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 is really stupid because i think techically it already + // does get inhearited but on line 55 in the test it expects + // 6 so we have to add 1 to this.age because im pretty sure + // this.age is set to 5 and then they call getolder in the test + // and expect 6 even though its not mentioned anywhere + return this.age + 1; + } +} + +class Cat extends Animal { + constructor(options) { + super(options); + this.name = options.name; + } + + meow() { + // complained about formatting, this is better because its template (whatever that means) + return `${this.name} meowed!`; + } +} module.exports = { User, diff --git a/src/es6.js b/src/es6.js index eb846ab..81f426a 100644 --- a/src/es6.js +++ b/src/es6.js @@ -1,56 +1,58 @@ -/* 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'; +// }; +// +// let isThisMyFavorite = isMyFavoriteFood(food); +// +// //---------------- +// //const, class, template literals, enhanced object literals (foo: foo, -> foo,) +// +// class User = { +// constructor(options) { +// this.username = options.username; +// this.password = options.password; +// } +// sayHi() { +// return this.username + ' says hello!'; +// }; +// } +// +// let username = 'JavaScriptForever'; +// let 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 */ diff --git a/src/objects.js b/src/objects.js index ba39c6c..89e34b0 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,21 +1,32 @@ // 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 + // Also a good resource + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/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 + // better resource https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values + + +// doesnt work, not sure why + 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 pairs = (obj) => { diff --git a/src/recursion.js b/src/recursion.js index a3e997e..db8f891 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,11 +3,19 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 2 3 5 8 13 ... // return the nth number in the sequence + + if (n < 2) { + return 1; + } + // recursive is a little neater than a loop + 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 === 0 || n === 1) { return 1; } + return n * nFactorial(n - 1); }; const checkMatchingLeaves = (obj) => {