From a4d6985fa0169a3b58eb393d08461feb81f5e64e Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Mon, 19 Mar 2018 17:28:26 -0400 Subject: [PATCH 1/8] completed callbacks.js and fixed all syntax errors --- src/callbacks.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/callbacks.js b/src/callbacks.js index 53917475..08676ab7 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -1,26 +1,41 @@ const firstItem = (arr, cb) => { // firstItem passes the first item of the given array to the callback function. + // arr[0] will call first item in array + cb(arr[0]); }; const getLength = (arr, cb) => { // getLength passes the length of the array into the callback. + cb(arr.length); }; const last = (arr, cb) => { // last passes the last item of the array into the callback. + cb(arr[arr.length - 1]); }; const sumNums = (x, y, cb) => { // sumNums adds two numbers (x, y) and passes the result to the callback. + const z = x + y; + cb(z); }; const multiplyNums = (x, y, cb) => { // multiplyNums multiplies two numbers and passes the result to the callback. + const z = x * y; + cb(z); }; 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. + for (let i = 0; i < list.length; i++) { + if (list[i] === item) { + cb(true); + } else { + cb(false); + } + } }; /* STRETCH PROBLEM */ From 20390144f92fee6c250904130a2c7a91bb985f29 Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Mon, 19 Mar 2018 18:27:22 -0400 Subject: [PATCH 2/8] completed each and worked on map array problems --- src/arrays.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 968a168b..776e6fe8 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -14,12 +14,18 @@ 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. + for (let i = 0; i < elements.length; i++) { + cb(elements[i]); + } }; const reduce = (elements, cb, startingValue) => { @@ -28,6 +34,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) => { From 50de7b0629a9ed1826535131884516bbf39e3d6f Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Mon, 19 Mar 2018 18:36:18 -0400 Subject: [PATCH 3/8] completed map problem --- src/arrays.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index 776e6fe8..5e77703b 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -23,9 +23,12 @@ 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 arrayOne = []; for (let i = 0; i < elements.length; i++) { - cb(elements[i]); + const endValue = cb(elements[i]); + arrayOne.push(endValue); } + return arrayOne; }; const reduce = (elements, cb, startingValue) => { From 52fbbb4c4092a22353d1468ec835de3536f4c808 Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Tue, 20 Mar 2018 14:45:30 -0400 Subject: [PATCH 4/8] added reduce solution & added hangout solution in comments --- src/arrays.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/arrays.js b/src/arrays.js index 5e77703b..7ddbbefe 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -37,9 +37,25 @@ 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. - + for (let i = 0; i < elements.length; i++) { + if (startingValue === undefined) { + startingValue = elements[0]; + i++; + } + startingValue = cb(startingValue, elements[i]); + } + return startingValue; }; + /* alt solution ``````````` + const newArr = elements.slice(); +let memo = startingValue || newArr.shift(); +each(newArr, (item) => { + memo = cb(memo, item); +}); +return memo; +}; +``````````````````````````` */ const find = (elements, cb) => { // Do NOT use .includes, to complete this function. // Look through each value in `elements` and pass each element to `cb`. From e4f8292e3f552a901d655d954985c43f9460737d Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Tue, 20 Mar 2018 14:53:42 -0400 Subject: [PATCH 5/8] finished filter and arrays.js --- src/arrays.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 7ddbbefe..743a2bba 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -61,12 +61,21 @@ 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]; + } }; 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 filterArr = []; + + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) filterArr.push(elements[i]); + } + return filterArr; }; /* STRETCH PROBLEM */ From 93a5b853cf6d290472bf58cb81cd575481dc9b01 Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Tue, 20 Mar 2018 16:30:23 -0400 Subject: [PATCH 6/8] completed objects.js --- src/objects.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/objects.js b/src/objects.js index 2898d4d4..6c036734 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,22 +5,37 @@ 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 + 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 + // this will go through the entire length of the keys! + for (let i = 0; i < Object.keys(obj).length; i++) { + obj[Object.keys(obj)[i]] = cb(Object.values(obj)[i]); + } + return obj; }; const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs + // this will go through all keys + const resultArr = []; + const keysArr = keys(obj); + const valuesArr = values(obj); + for (let i = 0; i < Object.keys(obj).length; i++) { + resultArr[i] = [keysArr[i], valuesArr[i]]; + } + return resultArr; }; /* STRETCH PROBLEMS */ From df6db25d84f45d999ef8295a68a1d8ec108067e8 Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Tue, 20 Mar 2018 16:46:11 -0400 Subject: [PATCH 7/8] completed counter and counterFactory --- src/closure.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/closure.js b/src/closure.js index 2a3cee37..9cb05154 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,17 +5,31 @@ 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() { + 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. + }; /* STRETCH PROBLEM */ From 8a3b4dfe06a269ad3dcde042d7ecf27d4cff268a Mon Sep 17 00:00:00 2001 From: Kevin Tran Date: Tue, 20 Mar 2018 17:38:31 -0400 Subject: [PATCH 8/8] added beginnings of limitfuncitonCallCount --- src/closure.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/closure.js b/src/closure.js index 9cb05154..c0ac2620 100644 --- a/src/closure.js +++ b/src/closure.js @@ -7,7 +7,7 @@ const counter = () => { // newCounter(); // 2 let count = 0; return () => { - ++count; + count++; return count; }; }; @@ -19,17 +19,24 @@ const counterFactory = () => { let count = 0; return { increment() { - return ++count; + return ++count; // adds one to counter }, decrement() { - return --count; + return --count; // removes one from counter } }; }; const limitFunctionCallCount = (cb, n) => { // Should return a function that invokes `cb`. // The returned function should only allow `cb` to be invoked `n` times. - + // So what we want is n to be the limiting factor for count + // therefore, count should always be less than n while it's counting up + let count = 0; + // will keep counting until count is equal to n then will stop + if (count < n) { + cb(); + count++; + } }; /* STRETCH PROBLEM */