From 79898ad121557642dfc410123ee6a4ecad0b2735 Mon Sep 17 00:00:00 2001 From: ConfusionOrb221 Date: Fri, 27 Sep 2019 17:38:53 -0400 Subject: [PATCH 1/4] went to bed for the night --- assignments/array-methods.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..95803dba6 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,21 +58,34 @@ 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((i) => { + fullNames.push({firstName : i.first_name, last_name : i.last_name}) +}); 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 = []; +firstNamesAllCaps = runners.map((i) =>{ + return i.first_name.toUpperCase(); +}); 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 = []; +runnersLargeSizeShirt = runners.filter((i) =>{ + if(i.shirt_size == "L") + return i; +}); 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; +runners.reduce((acumulator, runner) => { + ticketPriceTotal += runner.donation; +}); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== From 1848e1cc5813ff97052692ba2dfc8af4636b7e0a Mon Sep 17 00:00:00 2001 From: ConfusionOrb221 Date: Fri, 27 Sep 2019 22:23:54 -0400 Subject: [PATCH 2/4] push push --- assignments/array-methods.js | 23 ++++++++++++++++++++--- assignments/callbacks.js | 17 +++++++++++------ assignments/closure.js | 4 ++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 95803dba6..64ee922ff 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -92,7 +92,24 @@ console.log(ticketPriceTotal); // 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 - +let evenRunners = []; //checks if the id remainder is 0 a.k.a even and pushes it into evenrunners +runners.forEach((i) => { + if(i.id % 2 == 0){ + evenRunners.push(i); + } +}); +console.log(evenRunners); // Problem 2 - -// Problem 3 \ No newline at end of file +let oddRunners = []; +runners.map((i) =>{ + if(i.id % 2 != 1) + oddRunners.push(i); +}); +console.log(oddRunners); +// Problem 3 +let bigSpenders = []; +runners.filter((i) =>{ + if(i.donation >= 50) + bigSpenders.push(i); +}); +console.log(bigSpenders); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..7e8691bbe 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -40,29 +40,34 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { - // getLength passes the length of the array into the callback. + cb(arr.length); } function last(arr, cb) { - // last passes the last item of the array into the callback. + cb(arr.length - 1); } function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. + cb(x + y); } function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x * y); } 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. + cb(list.includes(item)); } /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { + var unique = array.reduce((a,b) =>{ + if(a.indexOf(b) < 0) a.push(b); + return a; + },[]); + + cb(unique); // removeDuplicates removes all duplicate values from the given array. // Pass the duplicate free array to the callback function. // Do not mutate the original array. diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..3dcfdca61 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -3,7 +3,11 @@ // Keep it simple! Remember a closure is just a function // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +var stuff = ["yes", "no", "maybe"]; +function addName(string){ + stuff.push(string); +} /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From cf4141fdc0ee5dbe49b35b59a115310fd06ffa07 Mon Sep 17 00:00:00 2001 From: ConfusionOrb221 Date: Tue, 1 Oct 2019 22:26:01 -0400 Subject: [PATCH 3/4] Did the stretch goal challenged Struggled a lot with challenge number 4 mainly because i didnt set a variable = to the function resulting in the mathy object to be recreated each time --- assignments/array-methods.js | 6 ++-- assignments/callbacks.js | 12 ++++---- assignments/closure.js | 56 ++++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 64ee922ff..cdac3792f 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -100,14 +100,14 @@ runners.forEach((i) => { }); console.log(evenRunners); // Problem 2 -let oddRunners = []; +let oddRunners = []; //checks if the id remainder is not 0 aka odd and pushes it runners.map((i) =>{ - if(i.id % 2 != 1) + if(i.id % 2 != 0) oddRunners.push(i); }); console.log(oddRunners); // Problem 3 -let bigSpenders = []; +let bigSpenders = []; //filters the big donation spenders runners.filter((i) =>{ if(i.donation >= 50) bigSpenders.push(i); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 7e8691bbe..438930e90 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -40,23 +40,23 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { - cb(arr.length); + return cb(arr.length); } function last(arr, cb) { - cb(arr.length - 1); + return cb(arr.length - 1); } function sumNums(x, y, cb) { - cb(x + y); + return cb(x + y); } function multiplyNums(x, y, cb) { - cb(x * y); + return cb(x * y); } function contains(item, list, cb) { - cb(list.includes(item)); + return cb(list.includes(item)); } /* STRETCH PROBLEM */ @@ -67,7 +67,7 @@ function removeDuplicates(array, cb) { return a; },[]); - cb(unique); + return cb(unique); // removeDuplicates removes all duplicate values from the given array. // Pass the duplicate free array to the callback function. // Do not mutate the original array. diff --git a/assignments/closure.js b/assignments/closure.js index 3dcfdca61..78a0f6bca 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -13,25 +13,69 @@ function addName(string){ // ==== Challenge 2: Implement a "counter maker" function ==== -const counterMaker = () => { // 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`. // NOTE: This `counter` function, being nested inside `counterMaker`, // "closes over" the `count` variable. It can "see" it in the parent scope! - // 3- Return the `counter` function. -}; -// Example usage: const myCounter = counterMaker(); -// myCounter(); // 1 -// myCounter(); // 2 + // 3- Return the `counter` function + +function counterMaker(){ + let count = 0; + const counter = function(){ + count = count + 1; + return count; + } + return counter; +} +const myCounter = counterMaker(); +console.log(myCounter()); // 1 +console.log(myCounter()); // 2 + + // ==== Challenge 3: Make `counterMaker` more sophisticated ==== // It should have a `limit` parameter. Any counters we make with `counterMaker` // will refuse to go over the limit, and start back at 1. +function counterMakerLimit(limit){ + let counts = 0; + const counterLimit = function(){ + if(counts == limit){ + counts = 1; + } + counts = counts + 1; + return counts; + } + return counterLimit; +} +const myCounterLimit = counterMakerLimit(); +console.log(myCounterLimit(5)); + + // ==== Challenge 4: Create a counter function with an object that can increment and decrement ==== const counterFactory = () => { + const mathy = { + counting: 0, + increment: function() { + return mathy.counting += 1; + }, + decrement: function() { + return mathy.counting -= 1; + } + } + return mathy; // 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. }; + + +var factory = counterFactory(); + +factory.increment(); +factory.increment(); +console.log(factory.counting); +factory.decrement(); +console.log(factory.counting); + From a6717cb97deffb27f5154b4cdd5f6b783d25a0d2 Mon Sep 17 00:00:00 2001 From: ConfusionOrb221 Date: Tue, 1 Oct 2019 22:31:40 -0400 Subject: [PATCH 4/4] edited challenge 3 this is to show that the counterlimit actualy worked --- assignments/closure.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/assignments/closure.js b/assignments/closure.js index 78a0f6bca..7eef634dd 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -42,15 +42,21 @@ function counterMakerLimit(limit){ let counts = 0; const counterLimit = function(){ if(counts == limit){ - counts = 1; + return counts = 1; } counts = counts + 1; return counts; } return counterLimit; } -const myCounterLimit = counterMakerLimit(); -console.log(myCounterLimit(5)); +var myCounterLimit = counterMakerLimit(5); +console.log(myCounterLimit()); +console.log(myCounterLimit()); +console.log(myCounterLimit()); +console.log(myCounterLimit()); +console.log(myCounterLimit()); +console.log(myCounterLimit()); +console.log(myCounterLimit()); // ==== Challenge 4: Create a counter function with an object that can increment and decrement ====