From 51d8cea7415eea6f1ca0fbc0fff3691cb723fccb Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 12:10:38 -0700 Subject: [PATCH 01/16] created a HOF and callback to return the array items length --- assignments/callbacks.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..fcf72e1cd 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -41,8 +41,15 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr); } +const arrLength = function(arr){ + return arr.length; +} + +console.log(getLength(items, arrLength)); + function last(arr, cb) { // last passes the last item of the array into the callback. } From 456d01606f7940863fc9e9b41ded12859f4ed2f2 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 12:29:17 -0700 Subject: [PATCH 02/16] added callbacks for addition, multiplication, and show last HOFs in callback.js --- assignments/callbacks.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index fcf72e1cd..9f3ad1fc7 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -48,20 +48,59 @@ const arrLength = function(arr){ return arr.length; } + +//////////////MY ATTEMPT at inline did not work//// +// function getLength(arr, arr => arr.length); +// const arrLength = arr => arr.length; + + console.log(getLength(items, arrLength)); + + + function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr); } +const arrLast = function(arr){ + return arr[arr.length -1]; +} + +console.log(last(items, arrLast)); + + + + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x, y); } +const addTwo = function(x,y){ + return x + y; +} + +console.log(sumNums(1, 2, addTwo)); + + + + + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x , y); +} + +const productTwo = function(x, y){ + return x * y; } +console.log(multiplyNums(3, 4, productTwo)); + + + function 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. From 1355f11c395796ce8054423008be00fda33175fb Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 12:48:57 -0700 Subject: [PATCH 03/16] added callback for contains function in callback.js, .includes will work with just a return or in an if statement but not if it is spelled wrong XD --- assignments/callbacks.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 9f3ad1fc7..56da1e7c4 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -104,8 +104,19 @@ console.log(multiplyNums(3, 4, productTwo)); function 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. + return cb(item, list); } +const runThroughList = function(item, list){ + return list.includes(item); + + +} + +console.log(contains('Pencil', items, runThroughList)); +console.log(contains('Eraser', items, runThroughList)); + + /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { From fbf86a1a8d0f1f9d2754ee6612544bec1223cc3b Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 13:04:22 -0700 Subject: [PATCH 04/16] added closure to closure.js, have to call the function with the outer scope to call the function within it that performs a closure by grabbing variables from the larger function --- assignments/closure.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..7bc225e92 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,6 +4,18 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +function spaceship(shipName){ + const position1 = "Navigator"; + const position2 = "Cook"; + + function lifepod(podNum){ + console.log(`${podNum} didn't have enough space for more than one person so that person is both the ${position1} and ${position2}.`) + }; + + lifepod("Pod5"); +} +spaceship("crazy horse"); + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From 1a9523a5d0d684daa6f1046d9123ed91086956a8 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 13:12:00 -0700 Subject: [PATCH 05/16] made a forEach for runners array so that i could push each of its items names to a new item of another array called fullNames --- assignments/array-methods.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..ebc0a7c5b 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,6 +58,9 @@ const runners = [ // ==== Challenge 1: Use .forEach() ==== // The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names and populate a new array called `fullNames`. This array will contain just strings. let fullNames = []; +runners.forEach(function(item){ + fullNames.push(`${item.first_name} ${item.last_name}`); +}) console.log(fullNames); // ==== Challenge 2: Use .map() ==== From 61058a962b7e76f6b63d88eff92c1578e54abd5b Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 13:16:02 -0700 Subject: [PATCH 06/16] added uppercase first names to array called firstNamesAllCaps, almost made another array to push to so that I could change the case before I pushed it to the proper array but saw in the documentation that I could use the method inline/on the value directly --- assignments/array-methods.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index ebc0a7c5b..8bd2b5563 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -66,6 +66,9 @@ console.log(fullNames); // ==== Challenge 2: Use .map() ==== // The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings. let firstNamesAllCaps = []; +runners.forEach(function(item){ + firstNamesAllCaps.push(item.first_name.toUpperCase()) +}) console.log(firstNamesAllCaps); // ==== Challenge 3: Use .filter() ==== From f26d3d4e0d2b932e4320f856d57bc351ac2fe0f1 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 14:02:20 -0700 Subject: [PATCH 07/16] completed challenge 3 of array-methods.js, had a hiccup using filter because I never gave it the parameter/reference the function would be looking at, I went straight into the property thinking i didn't need to --- assignments/array-methods.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 8bd2b5563..996eecddb 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -74,6 +74,10 @@ console.log(firstNamesAllCaps); // ==== Challenge 3: Use .filter() ==== // The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects. let runnersLargeSizeShirt = []; +runners.forEach(function(item){ + runnersLargeSizeShirt.push(runners.filter(item => item.shirt_size == "L")); //this wont work without the item => in the filter b/c you haven't given the filter anything to reference to create or look for item.shirt_size +}); + console.log(runnersLargeSizeShirt); // ==== Challenge 4: Use .reduce() ==== From b5509a9e3800eb2cfc68ceef45a9bbea22fade30 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 14:27:45 -0700 Subject: [PATCH 08/16] removed for each from filter challenge, it was creating copies of the filtered array --- assignments/array-methods.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 996eecddb..ac47f9fb7 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -74,15 +74,16 @@ console.log(firstNamesAllCaps); // ==== Challenge 3: Use .filter() ==== // The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects. let runnersLargeSizeShirt = []; -runners.forEach(function(item){ runnersLargeSizeShirt.push(runners.filter(item => item.shirt_size == "L")); //this wont work without the item => in the filter b/c you haven't given the filter anything to reference to create or look for item.shirt_size -}); + console.log(runnersLargeSizeShirt); // ==== Challenge 4: Use .reduce() ==== // The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable. let ticketPriceTotal = 0; + +ticketPriceTotal = runners.reduce((acc, currentVal) => {acc += currentVal.donation}); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== From 5ddf8a992f020c8d0736f0da70e4aee8cb5323a7 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 14:35:37 -0700 Subject: [PATCH 09/16] fixed challenge 4 of array-methods by removing curly brackets from arrow function --- assignments/array-methods.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index ac47f9fb7..d9b7c70c5 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -83,7 +83,10 @@ console.log(runnersLargeSizeShirt); // The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable. let ticketPriceTotal = 0; -ticketPriceTotal = runners.reduce((acc, currentVal) => {acc += currentVal.donation}); +// ticketPriceTotal = runners.reduce(function(acc, currentVal) {return acc += currentVal.donation}, 0); +// console.log(ticketPriceTotal); + +ticketPriceTotal = runners.reduce((acc, currentVal) => acc + currentVal.donation, 0); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== From 7821fa38dc0228bbb2b2dda90b264bac3eb89ed2 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 14:37:31 -0700 Subject: [PATCH 10/16] fixed challenge 2 of array-methods to use .map instead of forEach --- assignments/array-methods.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index d9b7c70c5..08bc113ab 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -66,9 +66,11 @@ console.log(fullNames); // ==== Challenge 2: Use .map() ==== // The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings. let firstNamesAllCaps = []; -runners.forEach(function(item){ - firstNamesAllCaps.push(item.first_name.toUpperCase()) -}) +// runners.forEach(function(item){ +// firstNamesAllCaps.push(item.first_name.toUpperCase()) +// }) + +firstNamesAllCaps = runners.map((item) => item.first_name.toUpperCase()); console.log(firstNamesAllCaps); // ==== Challenge 3: Use .filter() ==== From 0df60a50f94ad58a5b2787ec061ebca80abdad5d Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 14:44:29 -0700 Subject: [PATCH 11/16] made a new problem that required a filter but this time I just reassigned some saved memory to the outcome of the method instead of pushing to it with a push method --- assignments/array-methods.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 08bc113ab..7aa0358f1 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -94,7 +94,14 @@ console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== // Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above. -// Problem 1 +// Problem 1: We want to give everyone who donated over 200 dollars a gift basket, get a list of those generous businesses + +let givingBusinesses = []; + +givingBusinesses = runners.filter(item => item.donation > 200); + +console.log(givingBusinesses); + // Problem 2 From 8a060eea4da705daa5cf2d47c896d47832fae7f3 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 14:54:11 -0700 Subject: [PATCH 12/16] added a tax problem that could be solved with map to array-methods --- assignments/array-methods.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 7aa0358f1..615911389 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -94,15 +94,21 @@ console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== // Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above. -// Problem 1: We want to give everyone who donated over 200 dollars a gift basket, get a list of those generous businesses +// Problem 1: We want to give everyone who donated over 200 dollars a gift basket, get a list of those generous people -let givingBusinesses = []; +let givingPeople = []; -givingBusinesses = runners.filter(item => item.donation > 200); +givingPeople = runners.filter(item => item.donation > 200); -console.log(givingBusinesses); +console.log(givingPeople); -// Problem 2 +// Problem 2: actually we want the tax to know exactly how much tax is coming from each donation, make an array that shows how much each person is paying to a 17% tax + +let taxMan = []; + +taxMan = runners.map((item) => item.donation * .17); + +console.log(taxMan); // Problem 3 \ No newline at end of file From de69046feba4cf11ee1399529b71ceb7f3ac6c65 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 15:07:54 -0700 Subject: [PATCH 13/16] added last problem to array-methods, was overthinking finding the total number of the new array with reduce when all I needed was length --- assignments/array-methods.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 615911389..4cd90d840 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -111,4 +111,12 @@ taxMan = runners.map((item) => item.donation * .17); console.log(taxMan); -// Problem 3 \ No newline at end of file +// Problem 3: We want to know how many people wore 2XL, reduce them to a number! + +let totalMiddleWeights = 0; +let middleWeightsByName = []; +middleWeightsByName = runners.filter((item) => item.shirt_size == "2XL"); +console.log(middleWeightsByName); +totalMiddleWeights = middleWeightsByName.length; + +console.log(totalMiddleWeights); \ No newline at end of file From 84fcc69326fa3adfb6ff1d07256609c3a57a97c6 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 15:17:25 -0700 Subject: [PATCH 14/16] made test array for callback.js stretch, solved stretch using a filter and indexOf --- assignments/callbacks.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 56da1e7c4..9815105e7 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -119,8 +119,20 @@ console.log(contains('Eraser', items, runThroughList)); /* STRETCH PROBLEM */ +let testArray = [1, 1, 2, 4, 5, 6, 6, 7, 8] + function 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. + + return cb(array); } + +const removeSamsies = function(list){ + return list.filter(function(item, index){ + return list.indexOf(item) >= index; + }) +} + +console.log(removeDuplicates(testArray, removeSamsies)); From 6158adf5ed94e59773ea22b29dbf4f193bb9e3fe Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 16:23:53 -0700 Subject: [PATCH 15/16] sprint challenge 2 from closure.js solved with help from B.Teague, I was missing a return from my nested function --- assignments/closure.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/assignments/closure.js b/assignments/closure.js index 7bc225e92..6511e8dbf 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -11,7 +11,7 @@ function spaceship(shipName){ function lifepod(podNum){ console.log(`${podNum} didn't have enough space for more than one person so that person is both the ${position1} and ${position2}.`) }; - + lifepod("Pod5"); } spaceship("crazy horse"); @@ -22,6 +22,11 @@ spaceship("crazy horse"); // ==== Challenge 2: Implement a "counter maker" function ==== const counterMaker = () => { + let count = 0; + return function counter(){ //don't forget the damn RETURN + count += 1; + return count; + }; // IMPLEMENTATION OF counterMaker: // 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`! // 2- Declare a function `counter`. It should increment and return `count`. @@ -29,6 +34,13 @@ const counterMaker = () => { // "closes over" the `count` variable. It can "see" it in the parent scope! // 3- Return the `counter` function. }; + +const newCounter = counterMaker(); +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); + // Example usage: const myCounter = counterMaker(); // myCounter(); // 1 // myCounter(); // 2 From 5a32e62a768f90507a42a825acc4aa1d6c76d41a Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Tue, 17 Sep 2019 16:53:44 -0700 Subject: [PATCH 16/16] added limiter to count at count =4 --- assignments/closure.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assignments/closure.js b/assignments/closure.js index 6511e8dbf..e8bfd5b2e 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -23,10 +23,17 @@ spaceship("crazy horse"); // ==== Challenge 2: Implement a "counter maker" function ==== const counterMaker = () => { let count = 0; + return function counter(){ //don't forget the damn RETURN count += 1; - return count; + if (count >= 4){ + return count = count - 3; + }else{ + return count;} }; + + + // IMPLEMENTATION OF counterMaker: // 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`! // 2- Declare a function `counter`. It should increment and return `count`.