diff --git a/.eslintrc.json b/.eslintrc.json index 82ed6b2..edd742b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -16,6 +16,7 @@ "arrow-body-style": 0, "max-len": 0, "no-unused-vars": 0, - "no-useless-constructor": 0 + "no-useless-constructor": 0, + "import/no-unresolved": 0 } } diff --git a/src/arrays.js b/src/arrays.js index f24d6ef..33d9989 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -6,33 +6,70 @@ 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 newArray = []; + for (let i = 0; i < elements.length; i++) { + newArray[i] = cb(elements[i]); + } + return newArray; }; 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 i = 0; + if (memo === undefined) { + memo = elements[0]; + i++; + } + + 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])) { + 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 answer = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) { + answer.push(elements[i]); + } + } + return answer; }; const flatten = (elements) => { // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; + // x should return an array (5ms) + // ✕ should return a flattened array when given a nested array (1ms) + // ✕ should return a flattened array regardless of how deep the array nesting is (2ms) + const flatArray = []; + return flatArray; }; /* eslint-enable no-unused-vars, max-len */ diff --git a/src/es6.js b/src/es6.js index eb846ab..0fbe1f2 100644 --- a/src/es6.js +++ b/src/es6.js @@ -7,19 +7,19 @@ //---------------- // const, =>, default parameters, arrow functions default return statements using () -var food = 'pineapple'; +const food = 'pineapple'; -var isMyFavoriteFood = function(food) { +const 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 isThisMyFavorite = isMyFavoriteFood(food); //---------------- //const, class, template literals, enhanced object literals (foo: foo, -> foo,) -var User = function(options) { +let User = function(options) { this.username = options.username; this.password = options.password; this.sayHi = function() { @@ -27,10 +27,10 @@ var User = function(options) { }; } -var username = 'JavaScriptForever'; -var password = 'password'; +const username = 'JavaScriptForever'; +const password = 'password'; -var me = new User({ +const me = new User({ username: username, password: password, }); @@ -38,19 +38,19 @@ var me = new User({ // ---------------- // let, const, =>, ... (spread operator) -var addArgs = function () { - var sum = 0; - for (var i = 0; i < arguments.length; i++) { +const addArgs = function () { + 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); +const argsToCb = function (cb) { + const 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 result = argsToCb(addArgs, 1, 2, 3, 4, 5); //result should be 15 /* eslint-enable */ diff --git a/tests/arrays.test.js b/tests/arrays.test.js index 5033702..71a2c12 100644 --- a/tests/arrays.test.js +++ b/tests/arrays.test.js @@ -1,6 +1,7 @@ const arrayMethods = require('../src/arrays'); /* eslint-disable no-undef */ + describe('arrays', () => { describe('each', () => { it('should invoke cb on each array element', () => {