From 708c7995008223b049735a13a02fdff8136ca906 Mon Sep 17 00:00:00 2001 From: "Ayinde K. Williams" Date: Tue, 11 Sep 2018 17:00:37 -0500 Subject: [PATCH 1/4] Callbacks --- assignments/callbacks.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..229f36ff1 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,11 +1,13 @@ -// Create a callback function and invoke the function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems. +// Create a callback function and invoke the function to test your work. +//You have been provided an example of a problem and a solution to see how this works with our items array. +// Study both the problem and the solution to figure out the rest of the problems. const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* +/* + + //Given this problem: - //Given this problem: - function firstItem(arr, cb) { // firstItem passes the first item of the given array to the callback function. } @@ -22,18 +24,36 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; */ -function getLength(arr, cb) { - // getLength passes the length of the array into the callback. +function getLength(arr, cb,) { +return cb(arr); // getLength passes the length of the array into the callback. } +getLength(items.length, function() { + console.log(items.length) +}); + + + + + + + + + + function last(arr, cb) { - // last passes the last item of the array into the callback. +return cb(arr[3]); // last passes the last item of the array into the callback. } +last(items, function(last){ + console.log(last) +}) + function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. +return cb(arr) // sumNums adds two numbers (x, y) and passes the result to the callback. } +sumNums(items ) function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. } From 4ac960cad7d6b7d1bfb5a3c143ac6878695a75c9 Mon Sep 17 00:00:00 2001 From: "Ayinde K. Williams" Date: Tue, 11 Sep 2018 22:16:33 -0500 Subject: [PATCH 2/4] Figured out closures --- assignments/array-methods.js | 10 +++++----- assignments/callbacks.js | 4 ++++ assignments/closure.js | 7 ++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..731c99ae1 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -54,14 +54,14 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c {"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}]; // ==== 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 into a new array called fullName. -let fullName = []; +// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName. +let fullName = [`${first_name} ${last_name}`]; console.log(fullName); // ==== Challenge 2: Use .map() ==== // The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result -let allCaps = []; -console.log(allCaps); +let allCaps = [first_name.toUpperCase]; +console.log(allCaps); // ==== Challenge 3: Use .filter() ==== // The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result @@ -80,4 +80,4 @@ console.log(ticketPriceTotal); // Problem 2 -// Problem 3 \ No newline at end of file +// Problem 3 diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 229f36ff1..eb9210371 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -49,6 +49,10 @@ last(items, function(last){ console.log(last) }) + + + +//Start off ! function sumNums(x, y, cb) { return cb(arr) // sumNums adds two numbers (x, y) and passes the result to the callback. } diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..d2114810f 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,7 +1,12 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! - +const lastName = 'Bond'; +function greet() { + const firstName = 'James'; + alert(`The name's ${lastName}, ${firstName} ${lastName}`); +} +console.log(firstName) // ==== Challenge 2: Create a counter function ==== const counter = () => { // Return a function that when invoked increments and returns a counter variable. From caea626663cf96190360655f6f6f8a5c28a158e6 Mon Sep 17 00:00:00 2001 From: "Ayinde K. Williams" Date: Wed, 12 Sep 2018 09:24:43 -0500 Subject: [PATCH 3/4] All CLosures --- assignments/closure.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assignments/closure.js b/assignments/closure.js index d2114810f..df88d98e4 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -8,9 +8,12 @@ function greet() { } console.log(firstName) // ==== Challenge 2: Create a counter function ==== -const counter = () => { +const newCounter = counter(); => { +newCounter(); + // Return a function that when invoked increments and returns a counter variable. }; +console.log(counter) // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 From e871603ef4b998e1b163683e387ecd346f574e40 Mon Sep 17 00:00:00 2001 From: "Ayinde K. Williams" Date: Tue, 18 Sep 2018 17:13:35 -0500 Subject: [PATCH 4/4] . --- assignments/array-methods.js | 19 +++-- assignments/callbacks.js | 121 +++++++++++++------------------- assignments/closure.js | 130 +++++++++++++++++++++++++++-------- 3 files changed, 163 insertions(+), 107 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 731c99ae1..4ccd7a131 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -55,26 +55,35 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c // ==== 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 into a new array called fullName. -let fullName = [`${first_name} ${last_name}`]; +let fullName = []; +let conferenceNames = runners.forEach(function(i){ + fullName.push(i.first_name + " " + i.last_name); +}); console.log(fullName); // ==== Challenge 2: Use .map() ==== // The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result -let allCaps = [first_name.toUpperCase]; +let allCaps = runners.map((person) => { + return person.first_name.toUpperCase(); +}); console.log(allCaps); // ==== Challenge 3: Use .filter() ==== // The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result -let largeShirts = []; +let largeShirts = runners.filter((shirts) => { + return shirts.shirt_size === "L"; +}); console.log(largeShirts); // ==== Challenge 4: Use .reduce() ==== // The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result -let ticketPriceTotal = []; +let ticketPriceTotal = runners.reduce((donationTotal, donationAmount) => { + return donationTotal + donationAmount.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 .forEach(), .map(), .filter(), and .reduce(). I want you to think of a potential problems you could solve given the data set and the 5k fun run theme. Try to 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 eb9210371..4362bc689 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,76 +1,49 @@ -// Create a callback function and invoke the function to test your work. -//You have been provided an example of a problem and a solution to see how this works with our items array. -// Study both the problem and the solution to figure out the rest of the problems. - -const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; - -/* - - //Given this problem: - - function firstItem(arr, cb) { - // firstItem passes the first item of the given array to the callback function. +// ==== Challenge 1: Write your own closure ==== +// Write a simple closure of your own creation. Keep it simple! +function sayHello() { + let name = "Jim"; + function slang() { + // accessing myName outside this function using a closure + console.log("Yo " + name) } - - // Potential Solution: - function firstItem(arr, cb) { - return cb(arr[0]); - } - - firstItem(items, function(first) { - console.log(first) - }); - -*/ - - -function getLength(arr, cb,) { -return cb(arr); // getLength passes the length of the array into the callback. -} - -getLength(items.length, function() { - console.log(items.length) -}); - - - - - - - - - - -function last(arr, cb) { -return cb(arr[3]); // last passes the last item of the array into the callback. -} - -last(items, function(last){ - console.log(last) -}) - - - - -//Start off ! -function sumNums(x, y, cb) { -return cb(arr) // sumNums adds two numbers (x, y) and passes the result to the callback. -} - -sumNums(items ) -function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. -} - -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. -} - -/* 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. + // calling slang inside sayHello() + slang(); } +console.log(sayHello()); + + +// ==== Challenge 2: Create a counter function ==== +const counter = () => { + let count = 0; + return () => (++count); + // Return a function that when invoked increments and returns a counter variable. +}; + +// Stretch Solution + +// Example usage: const newCounter = counter(); +// newCounter(); // 1 +// newCounter(); // 2 +const newCounter = counter(); +console.log(newCounter()) // 1 +console.log(newCounter()) // 2 +console.log(newCounter()) // 3 +console.log(newCounter()) // 4 +console.log(newCounter()) // 5 + +// ==== Challenge 3: Create a counter function with an object that can increment and decrement ==== +const counterFactory = () => { + let count = 0; + return { + increment: () => (++count), + decrement: () => (--count), + }; + // 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 newCounterFactory = counterFactory(); +newCounterFactory.increment(); // 1 +newCounterFactory.decrement(); // 0 +newCounterFactory.decrement(); // -1 diff --git a/assignments/closure.js b/assignments/closure.js index df88d98e4..a759dfbf2 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,28 +1,102 @@ -// ==== Challenge 1: Write your own closure ==== -// Write a simple closure of your own creation. Keep it simple! - -const lastName = 'Bond'; -function greet() { - const firstName = 'James'; - alert(`The name's ${lastName}, ${firstName} ${lastName}`); -} -console.log(firstName) -// ==== Challenge 2: Create a counter function ==== -const newCounter = counter(); => { -newCounter(); - - // Return a function that when invoked increments and returns a counter variable. -}; -console.log(counter) -// Example usage: const newCounter = counter(); -// newCounter(); // 1 -// newCounter(); // 2 - -/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */ - -// ==== Challenge 3: 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. -}; +const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'yo-yo']; + +function firstItem(arr, cb) { + // firstItem passes the first item of the given array to the callback function. + console.log(cb(arr)) +} + +let first = function(items) { + return items[0]; +} + +firstItem(items,first); + +// Anonymous Function Solution: +// function firstItem(arr, cb) { +// cb(arr[0]); +// } +// firstItem(items, function(first) { +// console.log(first); +// }); + +function getLength(arr, cb) { + // getLength passes the length of the array into the callback. + console.log(cb(arr)); +} + +let arrayLength = function(items) { + return items.length; +} + +getLength(items,arrayLength); + +// Anonymous Function Solution: +// function getLength(arr, cb) { +// // getLength passes the length of the array into the callback. +// 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. + cb(arr[arr.length - 1]); +} + +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. + cb(x + y); +} + +sumNums(10, 15, function(total) { + console.log(total); +}); + +function multiplyNums(x, y, cb) { + // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x * y); +} + +multiplyNums(10, 15, function(total) { + console.log(total); +}); + +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. + for (let i = 0; i < list.length; i++) { + if (list[i] === item) { + return cb(true); + } + } + return cb(false); +} + +contains('Bats', items, function(flag) { + console.log(flag); // should be false +}); + +contains('Notebook', items, function(flag) { + console.log(flag); // should be true +}); + +/* STRETCH PROBLEM */ + +function removeDuplicates(arr, cb) { + const seen = {};// a map of the keys. This will hold the values as a bool if 'seen' in the given array. + for (let i = 0; i < arr.length; i++) { // loop over array list and assign the object a 'key' of the given item w/ a value of 'true' + seen[arr[i]] = true; // this will set the key/value pair of each item found in the array to `true`. + // It will not duplicate the item because Objects can only have one key of of each 'like' so. `yo-yo` will only be added 1 time etc. + } + cb(Object.keys(seen));// Object.keys generates a list as an array of each key in the object passed to it. + // Could do the same of each value with `Object.values` :) +} + +removeDuplicates(items, function(dupeFreeArray) { + console.log(dupeFreeArray); +});