From 17df285aecb2d2a44a868f37e988214de057d76d Mon Sep 17 00:00:00 2001 From: blakelower Date: Tue, 15 Oct 2019 18:50:29 +0700 Subject: [PATCH 1/5] did number 1 and 2 --- .vscode/settings.json | 3 +++ assignments/array-methods.js | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..6f3a2913e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..f7f0c8d16 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,12 +57,16 @@ 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 fullName = []; +runners.forEach(runner => fullName.push( `${runner.first_name} ${runner.last_name}`)); +console.log(fullName); // ==== 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 = []; +let firstNamesAllCaps = runners.map((currentItem, index, array) => { + currentItem.first_name = currentItem.first_name.toUpperCase(); + return currentItem; +}); console.log(firstNamesAllCaps); // ==== Challenge 3: Use .filter() ==== From 48badb340ac9f9ac79c6fb491114f9fa608d6d65 Mon Sep 17 00:00:00 2001 From: blakelower Date: Tue, 15 Oct 2019 18:54:25 +0700 Subject: [PATCH 2/5] did number 3 --- assignments/array-methods.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f7f0c8d16..b0e9a97a3 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -71,9 +71,13 @@ 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 = []; +let runnersLargeSizeShirt = runners.filter((currentItem) => { + return currentItem.shirt_size === "L" +}); 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; From 49c725e20a6a1c8696966042fcd0e155361d2345 Mon Sep 17 00:00:00 2001 From: blakelower Date: Tue, 15 Oct 2019 19:06:47 +0700 Subject: [PATCH 3/5] did number 4 --- assignments/array-methods.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index b0e9a97a3..e7a080472 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -63,7 +63,7 @@ console.log(fullName); // ==== 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.map((currentItem, index, array) => { +let firstNamesAllCaps = runners.map((currentItem) => { currentItem.first_name = currentItem.first_name.toUpperCase(); return currentItem; }); @@ -77,12 +77,17 @@ let runnersLargeSizeShirt = runners.filter((currentItem) => { 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; +let ticketPriceTotal = runners.reduce ((accumulator, currentItem) => { + return accumulator + currentItem.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. From 3ff4fae3dd3b834d38a1e3565dfbeb89103de5a4 Mon Sep 17 00:00:00 2001 From: blakelower Date: Tue, 15 Oct 2019 19:34:39 +0700 Subject: [PATCH 4/5] did questions 1 - 4 on callbacks --- assignments/callbacks.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..c882f36db 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -27,6 +27,8 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; const test1 = firstItem(items, item => `I love my ${item}!`); console.log(test1); // "I love my Pencil!" + + // TEST 2 (declaring callback before hand): @@ -40,22 +42,32 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { - // getLength passes the length of the array into the callback. + cb(arr.length); } +getLength(items, (itemLength) => { + console.log(itemLength); +}); function last(arr, cb) { - // last passes the last item of the array into the callback. + return cb (arr[arr.length-3]); } +last(items, function (lastItem){ + console.log(lastItem); +}); function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. + return x + y; } +console.log(sumNums(2,4, sumNums)); + function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. + return x * y; } +console.log(multiplyNums(3, 6, multiplyNums)); 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 0c2fce32a79c5b61495e29a929091177cad92585 Mon Sep 17 00:00:00 2001 From: blakelower Date: Tue, 15 Oct 2019 21:18:46 +0700 Subject: [PATCH 5/5] finished up the work --- assignments/array-methods.js | 19 ++++++++++++------ assignments/callbacks.js | 39 +++++++++++++++++++++++++++--------- assignments/closure.js | 25 +++++++++++++++++++---- 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index e7a080472..66fef3eef 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -84,15 +84,22 @@ let ticketPriceTotal = runners.reduce ((accumulator, currentItem) => { }, 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. // Problem 1 - +let lastName = []; +runners.forEach(runner => lastName.push(`${runner.last_name}`)); +console.log(lastName); // Problem 2 +let runnersDonations = runners.filter ((currentItem) => { + return currentItem.donation < 100 +}); +console.log(runnersDonations); -// Problem 3 \ No newline at end of file +// Problem 3 +let emailUpperCase = runners.map((currentItem) => { + currentItem.email = currentItem.email.toUpperCase(); + return currentItem; +}); +console.log(emailUpperCase); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c882f36db..f2a72dba7 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -56,21 +56,42 @@ last(items, function (lastItem){ }); function sumNums(x, y, cb) { - return x + y; + return cb (x,y); } -console.log(sumNums(2,4, sumNums)); +console.log(sumNums(2,4, (x,y)=> x + y)); -function multiplyNums(x, y, cb) { - return x * y; +function numbers(x, y, cb) { + return cb(x , y); } -console.log(multiplyNums(3, 6, multiplyNums)); +function multiply(x,y){ + return x * y +} +console.log(numbers(3, 6, multiply )); + 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(true); + } else { + return cb(false); + } +} +contains('Notebook', items, function(result){ + console.log(result); +}); + + + + + + + + + + + + /* STRETCH PROBLEM */ diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..6c28631ee 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,19 +4,36 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +let dogs = 'cute'; +function poodles (){ + const smallDogs = 'shis tzus'; + console.log(`The ${smallDogs} is the cutest!`); + function largeDogs (){ + const bigDogs = 'Rottweiler'; + console.log(`I heard that the ${bigDogs} can be killers but they are so adorable`); + } largeDogs(); +} +poodles (); + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ // ==== Challenge 2: Implement a "counter maker" function ==== -const counterMaker = () => { - // IMPLEMENTATION OF 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. -}; + // 3- Return the `counter` function + + + + + // Example usage: const myCounter = counterMaker(); // myCounter(); // 1 // myCounter(); // 2