From ab5b053a9f3d6a2947140c33c79c84b5cc39f9c9 Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Mon, 14 Oct 2019 19:12:39 -0700 Subject: [PATCH 01/11] completed: array.js challenge 1 --- 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 f3862361e..29d210cd7 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,7 +57,10 @@ 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 = []; +let fullNames = [] + +runners.forEach(runner => fullNames.push(`${runner.first_name} ${runner.last_name}`)); + console.log(fullNames); // ==== Challenge 2: Use .map() ==== From dd3caeecc28228e1ecf914103448826c972425e2 Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Mon, 14 Oct 2019 19:28:33 -0700 Subject: [PATCH 02/11] completed array-methods.js challenges 2 & 3 --- assignments/array-methods.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 29d210cd7..013736684 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -66,11 +66,13 @@ 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.map(runner => firstNamesAllCaps.push(runner.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 = []; +runners.filter(runner => { if (runner.shirt_size === "L") runnersLargeSizeShirt.push(runner)}); console.log(runnersLargeSizeShirt); // ==== Challenge 4: Use .reduce() ==== From 4472c04038ab08e3d2d42b589c20033f4a83d30c Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Mon, 14 Oct 2019 20:12:44 -0700 Subject: [PATCH 03/11] added challenge 5 problems to complete --- assignments/array-methods.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 013736684..8f2c581b1 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -65,19 +65,17 @@ 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.map(runner => firstNamesAllCaps.push(runner.first_name.toUpperCase())); +let firstNamesAllCaps = runners.map(runner => runner.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 = []; -runners.filter(runner => { if (runner.shirt_size === "L") runnersLargeSizeShirt.push(runner)}); +let runnersLargeSizeShirt = runners.filter(runner => { if (runner.shirt_size === "L") return runner}); 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((sum, runner) => sum + runner.donation, 0); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== From ee36fd19a62472012557302e24f692fb12dcebaa Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Mon, 14 Oct 2019 20:13:12 -0700 Subject: [PATCH 04/11] added challenge 5 problems to complete -corrected --- assignments/array-methods.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 8f2c581b1..147e8205c 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -59,6 +59,7 @@ const runners = [ // 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 = [] +//Figure out how to do this in the variable above. runners.forEach(runner => fullNames.push(`${runner.first_name} ${runner.last_name}`)); console.log(fullNames); @@ -82,7 +83,11 @@ 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 +// Create a new array where the runners are ordered by largest donation to smallest donation to give the poeple who donated more better visability. + // Problem 2 +// Find all the people who needed large shirts and donated more than 100 dollars so they can get replacement shirts for free. -// Problem 3 \ No newline at end of file +// Problem 3 +// Alphabetise the list by company name to make mailers to encourage more employees at each company to particpate next year. From 7f1014c19d852bfe64d854e77cb2fda93ae96214 Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Mon, 14 Oct 2019 20:34:53 -0700 Subject: [PATCH 05/11] completed all of array-methods.js challenges --- assignments/array-methods.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 147e8205c..bf1a8bd6d 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -83,11 +83,16 @@ 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 -// Create a new array where the runners are ordered by largest donation to smallest donation to give the poeple who donated more better visability. - +// Find the average donation. +let averageDonation = runners.reduce((sum, runner) => Math.round(sum + (runner.donation) / runners.length), 0); +console.log(averageDonation); // Problem 2 -// Find all the people who needed large shirts and donated more than 100 dollars so they can get replacement shirts for free. +// Find all the people who needed large shirts and donated 100 dollars or more so they can get replacement shirts for free. +let freeShirtReplacements = runners.filter(runner => { if (runner.shirt_size === "L" && runner.donation >= 100) return runner}); +console.log(freeShirtReplacements); // Problem 3 -// Alphabetise the list by company name to make mailers to encourage more employees at each company to particpate next year. +// Create an alphabetised list of company names to make mailers to encourage more employees at each company to particpate next year. +let spamTheseCompanies = runners.map(runner => runner.company_name).sort(); +console.log(spamTheseCompanies); \ No newline at end of file From ecc9ccab7bc18af5f09c586f6d07c5c7b1258cd9 Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Mon, 14 Oct 2019 21:28:47 -0700 Subject: [PATCH 06/11] completed callbacks.js challenges --- assignments/callbacks.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..7e193567b 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -38,28 +38,50 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ +const testArr = [1, 2, 3, 4, 5, 6, 7, 8, "apples"] +const newVar = [] +function testcb(v) { + newVar.push(v); +} function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr.length); } function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length - 1]); } + + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + let sum = x + y; + return cb(sum); } + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + let product = x * y; + return cb(product); } 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. + let checkItem = list.filter(el => el === item) + if (checkItem !== []) { + cb(checkItem); + } } + + +contains(1, testArr, testcb); +console.log(newVar); /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { From 8ed6a84b73ab317bd33a3639cbbfaf31312f786c Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Mon, 14 Oct 2019 21:37:36 -0700 Subject: [PATCH 07/11] completed closure.js challenge --- assignments/callbacks.js | 2 -- assignments/closure.js | 8 ++++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 7e193567b..443b58dfd 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -78,8 +78,6 @@ function contains(item, list, cb) { } } - - contains(1, testArr, testcb); console.log(newVar); /* STRETCH PROBLEM */ diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..ab93cb6cc 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,6 +4,14 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +function thisIsTheOuterScope() { + let aVariable = "foo"; + function thisIsTheInnerScope() { + return aVariable + " bar"; + } + console.log(thisIsTheInnerScope()); +} +thisIsTheOuterScope(); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From 6529f0549319d0e5c9e48e400d086451577d8ac8 Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Tue, 15 Oct 2019 14:56:19 -0700 Subject: [PATCH 08/11] finished all closure stretch goals --- assignments/closure.js | 48 ++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/assignments/closure.js b/assignments/closure.js index ab93cb6cc..b14707b37 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -17,25 +17,47 @@ thisIsTheOuterScope(); // ==== 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. -}; +// 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 +const counterMaker = () => { + let count = 0; + const counter = () => count += 1; + return counter; +}; +const myCounter = counterMaker(); // ==== 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. +const counterMakerLimit = (limit) => { + let count = 0; + const counter = () => (count < limit ? count += 1 : count = 1); + return counter; +}; +const myNewCounter = counterMakerLimit(3); -// ==== Challenge 4: Create a counter function with an object that can increment and decrement ==== -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. +// // ==== Challenge 4: Create a counter function with an object that can increment and decrement ==== +// 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. +const counterFactory = (value) => { + let counter = 0; + const methodObject = { + increment: () => counter += 1, + decrement: () => counter -= 1, + } + if (value === "plus") { + return methodObject.increment; + } else if (value === "minus") { + return methodObject.decrement; + } }; +const myEvenNewerCounter = counterFactory("plus"); +const myNewestCounter = counterFactory("minus"); \ No newline at end of file From 455e99eb038bb7809962eb22fe0fef47620bb09a Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Tue, 15 Oct 2019 15:47:58 -0700 Subject: [PATCH 09/11] callback.js stretch complete --- assignments/callbacks.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 443b58dfd..c65479f7e 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -38,7 +38,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ -const testArr = [1, 2, 3, 4, 5, 6, 7, 8, "apples"] +const testArr = [1, 2, "apples", 3, 4, 5, "pear", 6, 7, 8, 8, "apples"] const newVar = [] function testcb(v) { newVar.push(v); @@ -72,18 +72,21 @@ function multiplyNums(x, y, cb) { 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. - let checkItem = list.filter(el => el === item) - if (checkItem !== []) { - cb(checkItem); - } + return cb(list.some(listItem => listItem === item)); } -contains(1, testArr, testcb); -console.log(newVar); +// contains("apple", testArr, testcb); +// console.log(newVar); + /* STRETCH PROBLEM */ 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. + let newArray = [... new Set(array)]; + return cb(newArray); } +// removeDuplicates(testArr, testcb); +// console.log(newVar); +// console.log(testArr); \ No newline at end of file From 2fe4cff64e67b6f6089b67a46d3d16f11c2b862d Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Tue, 15 Oct 2019 15:53:09 -0700 Subject: [PATCH 10/11] some final code cleanup --- assignments/array-methods.js | 2 -- assignments/callbacks.js | 13 +++++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index bf1a8bd6d..8b83a29ca 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,8 +58,6 @@ 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 = [] - -//Figure out how to do this in the variable above. runners.forEach(runner => fullNames.push(`${runner.first_name} ${runner.last_name}`)); console.log(fullNames); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c65479f7e..0ed592552 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -38,11 +38,11 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ -const testArr = [1, 2, "apples", 3, 4, 5, "pear", 6, 7, 8, 8, "apples"] -const newVar = [] -function testcb(v) { - newVar.push(v); -} +// const testArr = [1, 2, "apples", 3, 4, 5, "pear", 6, 7, 8, 8, "apples"] +// const newVar = [] +// function testcb(v) { +// newVar.push(v); +// } function getLength(arr, cb) { // getLength passes the length of the array into the callback. @@ -54,15 +54,12 @@ function last(arr, cb) { return cb(arr[arr.length - 1]); } - - function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. let sum = x + y; return cb(sum); } - function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. let product = x * y; From 76be71a076d8f9da869dd3d4ddd91820cb469c73 Mon Sep 17 00:00:00 2001 From: michelangelo17 Date: Tue, 15 Oct 2019 15:56:43 -0700 Subject: [PATCH 11/11] updated readme, ready for pull request --- README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 143243273..e04096861 100644 --- a/README.md +++ b/README.md @@ -7,36 +7,36 @@ With some basic JavaScript principles in hand, we can now expand our skills out **Follow these steps to set up and work on your project:** -* [ ] Create a forked copy of this project. -* [ ] Add your team lead as collaborator on Github. -* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!). -* [ ] Create a new branch: git checkout -b ``. -* [ ] Implement the project on your newly created `` branch, committing changes regularly. -* [ ] Push commits: git push origin ``. +* [X] Create a forked copy of this project. +* [X] Add your team lead as collaborator on Github. +* [X] Clone your OWN version of the repository (Not Lambda's by mistake!). +* [X] Create a new branch: git checkout -b ``. +* [X] Implement the project on your newly created `` branch, committing changes regularly. +* [X] Push commits: git push origin ``. **Follow these steps for completing your project.** -* [ ] Submit a Pull-Request to merge Branch into master (student's Repo). **Please don't merge your own pull request** -* [ ] Add your team lead as a reviewer on the pull-request +* [X] Submit a Pull-Request to merge Branch into master (student's Repo). **Please don't merge your own pull request** +* [X] Add your team lead as a reviewer on the pull-request * [ ] Your team lead will count the project as complete by merging the branch back into master. ## Task 1: Higher Order Functions and Callbacks This task focuses on getting practice with higher order functions and callback functions by giving you an array of values and instructions on what to do with that array. -* [ ] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions. +* [X] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions. -* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. +* [X] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. ## Task 2: Array Methods Use `.forEach()`, `.map()`, `.filter()`, and `.reduce()` to loop over an array with 50 objects in it. The [array-methods.js](assignments/array-methods.js) file contains several challenges built around a fundraising 5K fun run event. -* [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file. +* [X] Review the contents of the [array-methods.js](assignments/array-methods.js) file. -* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. +* [X] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. -* [ ] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it. +* [X] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it. ## Task 3: Closures @@ -44,10 +44,10 @@ We have learned that closures allow us to access values in scope that have alrea **Hint: Utilize debugger statements in your code in combination with your developer tools to easily identify closure values.** -* [ ] Review the contents of the [closure.js](assignments/closure.js) file. -* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. +* [X] Review the contents of the [closure.js](assignments/closure.js) file. +* [X] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. ## Stretch Goals -* [ ] Go back through the stretch problems that you skipped over and complete as many as you can. -* [ ] Look up what an IIFE is in JavaScript and experiment with them \ No newline at end of file +* [X] Go back through the stretch problems that you skipped over and complete as many as you can. +* [X] Look up what an IIFE is in JavaScript and experiment with them \ No newline at end of file