From dfb8fa1bc54a8b15795a4ab5b5034e7432d49d67 Mon Sep 17 00:00:00 2001 From: Conary36 Date: Sat, 5 Oct 2019 12:28:52 -0400 Subject: [PATCH 1/3] initial commit --- assignments/array-methods.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..e446c659e 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,17 +57,38 @@ 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 = []; -console.log(fullNames); + let fullNames = []; + + fullNames = runners.forEach(runners => { + console.log(runners.first_name,runners.last_name); + }); + +// for(let i = 0; i < runners.length; i++){ +// let mappedObj = {}; +// mappedObj.first_name = runners[i].first_name; +// mappedObj.last_name = runners[i].last_name; +// fullNames.push(mappedObj); +// mappedObj = {}; +// } // ==== 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((runners) => { + return{ + 'first': runners.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((runners) => { + return runners.shirt_size === 'L'; +}); + console.log(runnersLargeSizeShirt); // ==== Challenge 4: Use .reduce() ==== From 22b41f7f221a3c682195243ab965657bd0209d1a Mon Sep 17 00:00:00 2001 From: Conary36 Date: Sun, 6 Oct 2019 14:58:11 -0400 Subject: [PATCH 2/3] array methods update --- assignments/array-methods.js | 7 ++++++- assignments/callbacks.js | 24 ++++++++++++++++++++++++ assignments/closure.js | 14 +++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index e446c659e..25a459c4d 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -94,10 +94,15 @@ 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((total,runners) =>{ + return total + runners.donation; + }, 0); + 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. +// Now that you have used .foEach(), .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 diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..310b2aeca 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -41,24 +41,48 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr.length); } +getLength(items, function(length){ + console.log(length); +}); function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[3]); } +last(items, function(lastNumber){ + console.log(lastNumber); +}); function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x,y); } +sumNums(4,5, function(x,y){ + console.log(x + y); +}); function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x,y); } + multiplyNums(5,5, function(x,y){ + console.log(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. + if(list.includes(item)){ + return cb(1); + }else{ + return cb(0); + } } +contains('Gum', items, function(answer){ + console.log(answer); +}) /* STRETCH PROBLEM */ diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..b839f5062 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -3,8 +3,20 @@ // 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. +let numbers = [23, 24, 45, 46]; - +function question(answer){ + //What number is you favorite number? + const myQuestion = 'What number is your favorite number?'; + + return myQuestion + " " + answer; + //return myQuestion + numbers[answer] //option for embedded function. +} +//console.log(question(3)); +function answer(i){ + return numbers[i] +} + console.log(question(answer(2))); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From 4759011c96662336a2da03acda5917269f37582b Mon Sep 17 00:00:00 2001 From: Conary36 Date: Tue, 8 Oct 2019 21:26:33 -0400 Subject: [PATCH 3/3] attempt at closure stretch problem with no success --- assignments/closure.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/assignments/closure.js b/assignments/closure.js index b839f5062..a364e7a0b 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -28,7 +28,27 @@ const counterMaker = () => { // 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. -}; + let count = 0; + function counter(){ + return count++; //increasing count + } + return counter(); + }; + + const myCounter = counterMaker(); + + console.log(myCounter); // 1 + + console.log(myCounter); // 2 + + console.log(myCounter); // 3 + + console.log(myCounter); // 4 + console.log(myCounter); // 5 + + console.log(myCounter); // 1 + + //console.log(counterMaker); // Example usage: const myCounter = counterMaker(); // myCounter(); // 1 // myCounter(); // 2