From 79280d22b2c89383f2a4c6540aa5fc1db1a36b7d Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Mon, 19 Mar 2018 22:26:03 -0700 Subject: [PATCH 1/2] callbacks complete, arrays in progress --- src/arrays.js | 29 +++++++++++++++++++++++++++++ src/callbacks.js | 21 +++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 968a168b..5f35a25f 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -14,12 +14,20 @@ const each = (elements, cb) => { // This only needs to work with arrays. // You should also pass the index into `cb` as the second argument // based off http://underscorejs.org/#each + for (let i = 0; i < elements.length; i++) { + cb(elements[i], i); + } }; const map = (elements, cb) => { // Do NOT use .map, to complete this function. // Produces a new array of values by mapping each value in list through a transformation function (iteratee). // Return the new array. + const newArr = []; + for (let i = 0; i < elements.length; i++) { + newArr.push(cb(elements[i])); + } + return newArr; }; const reduce = (elements, cb, startingValue) => { @@ -28,6 +36,7 @@ const reduce = (elements, cb, startingValue) => { // Elements will be passed one by one into `cb` along with the `startingValue`. // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument. // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value. + }; const find = (elements, cb) => { @@ -35,12 +44,32 @@ 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. + let test = false; + let index = 0; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) { + test = true; + index = i; + } + } + + if (test === true) { + return elements[index]; + } + return 'undifined'; }; const filter = (elements, cb) => { // Do NOT use .filter, to complete this function. // 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 newArr = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i]) === true) { + newArr.push(elements[i]); + } + return newArr; + } }; /* STRETCH PROBLEM */ diff --git a/src/callbacks.js b/src/callbacks.js index 53917475..2269268a 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -1,26 +1,43 @@ const firstItem = (arr, cb) => { // firstItem passes the first item of the given array to the callback function. + const item = arr[0]; + cb(item); }; const getLength = (arr, cb) => { // getLength passes the length of the array into the callback. + const length = arr.length; + cb(length); }; const last = (arr, cb) => { // last passes the last item of the array into the callback. + const lastItem = arr[arr.length - 1]; + cb(lastItem); }; const sumNums = (x, y, cb) => { - // sumNums adds two numbers (x, y) and passes the result to the callback. + // sumNums adds two numbers (x, y) and passes the result to the callback + const sum = x + y; + cb(sum); }; const multiplyNums = (x, y, cb) => { // multiplyNums multiplies two numbers and passes the result to the callback. + const prod = x * y; + cb(prod); }; const contains = (item, list, cb) => { // contains checks if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. + let test = false; + for (let i = 0; i <= list.length; i++) { + if (item === list[i]) { + test = true; + } + } + cb(test); }; /* STRETCH PROBLEM */ @@ -28,7 +45,7 @@ const contains = (item, list, cb) => { const removeDuplicates = (array, cb) => { // removeDuplicates removes all duplicate values from the given array. // Pass the duplicate free array to the callback function. - // Do not mutate the original array. + // Do not mutate the original array }; /* eslint-enable */ From 082cb19a1c712ac4b7dd75481c57a4fb54866f94 Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Wed, 21 Mar 2018 12:24:10 -0700 Subject: [PATCH 2/2] arrays complete, closures incomplete --- src/arrays.js | 18 +++++++++++++++++- src/closure.js | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index 5f35a25f..f7c3a571 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -36,7 +36,11 @@ const reduce = (elements, cb, startingValue) => { // Elements will be passed one by one into `cb` along with the `startingValue`. // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument. // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value. - + const elementsCopy = elements.slice(); + let memo = startingValue || elementsCopy.shift(); + each(elementsCopy, (item) => { + memo = cb(memo, item); + }); }; const find = (elements, cb) => { @@ -77,6 +81,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]; + const res = []; + const func = (arr) => { + for (let i = 0; i < arr.length; i++) { + if (Array.isArray(arr[i])) { + func(arr[i]); + } else { + res.push(arr[i]); + } + } + }; + func(elements); + return res; }; /* eslint-enable no-unused-vars, max-len */ diff --git a/src/closure.js b/src/closure.js index 2a3cee37..4a907cbb 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,12 +5,28 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let count = 0; + return () => { + count++; + 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: () => { + count++; + return count; + }, + decrement: () => { + count--; + return count; + } + }; }; const limitFunctionCallCount = (cb, n) => {