From eee7156e03d3dbd3d297e2f169be4188b35d57c0 Mon Sep 17 00:00:00 2001 From: Shawn Stewart Date: Mon, 19 Mar 2018 18:28:30 -0500 Subject: [PATCH 1/4] Partial completion of JavaScript I. --- src/arrays.js | 30 ++++++++++++++++++++++++++++++ src/callbacks.js | 11 +++++++++++ src/objects.js | 12 ++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 968a168b..abe702db 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,15 @@ 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. + let reduced = ''; + if (startingValue === undefined) { + startingValue = 0; + } + for (let i = startingValue; i < elements.length; i++) { + cb(startingValue, elements[i]); + reduced += elements[i]; + } + return reduced; }; const find = (elements, cb) => { @@ -35,12 +52,25 @@ 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) => { // 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 filtered = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) { + filtered.push(elements[i]); + } + } + return filtered; }; /* STRETCH PROBLEM */ diff --git a/src/callbacks.js b/src/callbacks.js index 53917475..fa0c546e 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -1,26 +1,37 @@ const firstItem = (arr, cb) => { // firstItem passes the first item of the given array to the callback function. + 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.length - 1); }; const sumNums = (x, y, cb) => { // sumNums adds two numbers (x, y) and passes the result to the callback. + cb(x + y); }; const multiplyNums = (x, y, cb) => { // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x * y); }; 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 < item.length; i++) { + if (list[i] === item) { + return cb(true); + } + } + return cb(false); }; /* STRETCH PROBLEM */ diff --git a/src/objects.js b/src/objects.js index 2898d4d4..ac4394c8 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,19 +5,31 @@ 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 + let objEntries = Object.entries(obj); + console.log(Object.entries(obj)); }; +const anObject = { + name: 'Shawn', + age: 21, + likesJavascript: true +}; + +mapObject(anObject); + const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs From f1ac461543f7fae0bc762e05678d131effea66d1 Mon Sep 17 00:00:00 2001 From: Shawn Stewart Date: Tue, 20 Mar 2018 03:13:22 -0500 Subject: [PATCH 2/4] Made further progress. --- src/objects.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/objects.js b/src/objects.js index ac4394c8..cec46cc9 100644 --- a/src/objects.js +++ b/src/objects.js @@ -18,18 +18,13 @@ const 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 - let objEntries = Object.entries(obj); - console.log(Object.entries(obj)); -}; + const newObj = {}; + for (let i = 0; i < obj.length; i++) { -const anObject = { - name: 'Shawn', - age: 21, - likesJavascript: true + } + return newObj; }; -mapObject(anObject); - const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs From 5bc04801e54b1b1bc0945bfc3b3150d8cd4ba5de Mon Sep 17 00:00:00 2001 From: Shawn Stewart Date: Tue, 20 Mar 2018 18:31:39 -0500 Subject: [PATCH 3/4] Completed objects.js / Partial completion closure.js --- src/callbacks.js | 3 ++- src/closure.js | 5 +++++ src/objects.js | 7 ++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/callbacks.js b/src/callbacks.js index fa0c546e..1f1243ca 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -10,7 +10,8 @@ const getLength = (arr, cb) => { const last = (arr, cb) => { // last passes the last item of the array into the callback. - cb(arr.length - 1); + const myLast = arr.length - 1; + return cb(myLast); }; const sumNums = (x, y, cb) => { diff --git a/src/closure.js b/src/closure.js index 2a3cee37..605ee492 100644 --- a/src/closure.js +++ b/src/closure.js @@ -5,6 +5,11 @@ const counter = () => { // Example: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + let myCount = 0; + return () => { + myCount++; + return myCount; + }; }; const counterFactory = () => { diff --git a/src/objects.js b/src/objects.js index cec46cc9..5822fb80 100644 --- a/src/objects.js +++ b/src/objects.js @@ -18,16 +18,17 @@ const 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 + // Object.keys(obj).forEach(x => (obj[x] = cb(obj[x]))); + // return obj; const newObj = {}; - for (let i = 0; i < obj.length; i++) { - - } + Object.keys(obj).forEach(x => (newObj[x] = cb(obj[x]))); return newObj; }; const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs + return Object.entries(obj); }; /* STRETCH PROBLEMS */ From 1e705178b7abd1fade29b72d63f5df00032c3a79 Mon Sep 17 00:00:00 2001 From: Shawn Stewart Date: Tue, 20 Mar 2018 19:18:57 -0500 Subject: [PATCH 4/4] Continuation closure.js --- src/closure.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/closure.js b/src/closure.js index 605ee492..3c653d25 100644 --- a/src/closure.js +++ b/src/closure.js @@ -16,11 +16,39 @@ 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 myCount = 0; + const obj = { + increment: () => { + myCount++; + return myCount; + }, + decrement: () => { + myCount--; + return myCount; + } + }; + + return obj; }; const limitFunctionCallCount = (cb, n) => { // Should return a function that invokes `cb`. // The returned function should only allow `cb` to be invoked `n` times. + // let myCount = 0; + // return () => { + // if (myCount < n) { + // myCount++; + // return cb(); + // } + // }; + return () => { + const myCount = 0; + return () => { + if (myCount < n) { + cb(); + } + }; + }; }; /* STRETCH PROBLEM */