From 5591a0dcb8dc9c05e5668bd348775e881d90cc05 Mon Sep 17 00:00:00 2001 From: John Nweke Date: Tue, 15 Oct 2019 15:03:23 -0400 Subject: [PATCH 1/4] First attemrpt at Callbacks --- assignments/callbacks.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..e74aba0b1 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,7 +2,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* + /* // GIVEN THIS PROBLEM: @@ -41,24 +41,51 @@ 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); +}; + +// TEST NOT WORKING! +// const test4 = showLength (items, getLength) { +// return `${getLength} is how long ${items} array is`; +// } +// console.log(test4); 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. -} + return cb(x + y); +}; function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. -} + return 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. -} + + // TRIED USING A FILTER + // function cb = list.filter((itemIterator) + // {return true + // }); + + function cb (item, list) { + for (i = 0; i < list.length; i++) { + if (list[i] === item){ + return true; + } else { + return false; + }// End of If Statement + }// End of For Loop + }; //end of Callback +}; // end of main funtion + + /* STRETCH PROBLEM */ From 9d9157d0f487cf5fd85c5e00c3915bd186697117 Mon Sep 17 00:00:00 2001 From: John Nweke Date: Tue, 15 Oct 2019 20:40:16 -0400 Subject: [PATCH 2/4] Completed Array Methods work --- assignments/array-methods.js | 21 +++++++++++++++++++++ assignments/callbacks.js | 11 ++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..ce4d2c1b6 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,21 +58,42 @@ 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(function (runnerObj){ + return fullNames.push(`${runnerObj.first_name} ${runnerObj.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(function(runnerObj){ + return runnerObj.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(function(runnerObj){ + if (runnerObj.shirt_size === 'L' || runnerObj.shirt_size === 'XL' || runnerObj.shirt_size === '2XL' || runnerObj.shirt_size === '3XL' || runnerObj.shirt_size === '4XL') { + return true; + } +}); + 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; +//Use Map to extract the donations to a new array +runnerDonations = runners.map(function(runnerObj){ + return runnerObj.donation; +}); +//Use reduce to tally up the new array +ticketPriceTotal = runnerDonations.reduce(function(accumulator, currentValue){ + return (accumulator + currentValue); +}); +//Print out the solution console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== diff --git a/assignments/callbacks.js b/assignments/callbacks.js index e74aba0b1..5d08fb962 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -38,12 +38,17 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ - +//My CODE STARTS HERE function getLength(arr, cb) { // getLength passes the length of the array into the callback. - return cb(arr.length); + cb(arr.length); }; + +getLength(items, (lengthoflist) => { + console.log(lengthoflist); +}); + // TEST NOT WORKING! // const test4 = showLength (items, getLength) { // return `${getLength} is how long ${items} array is`; @@ -68,7 +73,7 @@ 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. - + // TRIED USING A FILTER // function cb = list.filter((itemIterator) // {return true From 79679c22efe4735952611321ced4f33194643b75 Mon Sep 17 00:00:00 2001 From: John Nweke Date: Tue, 15 Oct 2019 21:07:02 -0400 Subject: [PATCH 3/4] completed closure function --- assignments/closure.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..4745570fe 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,7 +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. - + let dance = function (partner1, partner2, typeOfDance) { + // partner1 = function () { + // return `I am ${partner1}!`; + // }; + return `I am ${partner2}, and I don't like ${typeOfDance}ing with ${partner1}!`; + }; + console.log (dance ('Timothy', 'Elizabeth', 'waltz')); + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From deb622cedd43a8647b497be722cbb642a0cc1fe1 Mon Sep 17 00:00:00 2001 From: John Nweke Date: Wed, 16 Oct 2019 00:45:49 -0400 Subject: [PATCH 4/4] I am going to BED! --- .vscode/settings.json | 7 +++++++ assignments/array-methods.js | 32 ++++++++++++++++++++++++++++++-- assignments/callbacks.js | 2 ++ assignments/closure.js | 31 +++++++++++++++++++++++++++---- 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..fbced5738 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "editor.fontFamily": "Fantasque Sans Mono, Hack, Envy Code R VS, Menlo, OperatorMono-Book, SF Pro, Inconsolata, Anonymous Pro, Monaco, 'Courier New', monospace", + "editor.fontLigatures": true, + "editor.fontSize": 15, + "terminal.integrated.fontFamily": "Hack, Envy Code R VS, Menlo, OperatorMono-Book, SF Pro, Inconsolata, Anonymous Pro, Monaco, 'Courier New', monospace", + "terminal.integrated.fontSize": 11 +} \ No newline at end of file diff --git a/assignments/array-methods.js b/assignments/array-methods.js index ce4d2c1b6..a3d05cec9 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -99,8 +99,36 @@ 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 +// Problem 1: Create Runners Full Names and Donations in a string + +let fullNameAndDonations =[]; +runners.forEach(runnerObj => { + fullNameAndDonations.push(`${runnerObj.first_name} ${runnerObj.last_name} gave $${runnerObj.donation}.`); +}); +//Print out +console.log(fullNameAndDonations); // Problem 2 +//Big Givers: Filter every Runner who gave above $200 so we can send them a personalized Thank You Card. +let bigDonors = runners.filter(runnerObj => runnerObj.donation >= 200); +//Print out +console.log(bigDonors); + +// Problem 3 +//List out all the companies and find out how many runners came from each Company +//String template: ____ people from ____ company participated. +let runnerCompanies = runners + .map(runnerObj => runnerObj.company_name) + //Sorting all Companies alphabetically + .sort(); +//Print out +console.log (runnerCompanies); +//Counter for Each Company +let runnerCompanyNumbers = runnerCompanies.forEach(runnerObj => { +//if a===b, add 1; +//else skip to next and resume counter +}); + + + -// Problem 3 \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 5d08fb962..c93cd574f 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -99,3 +99,5 @@ function removeDuplicates(array, cb) { // Pass the duplicate free array to the callback function. // Do not mutate the original array. } + +//Chech Arrayzing. \ No newline at end of file diff --git a/assignments/closure.js b/assignments/closure.js index 4745570fe..c0cfe32ff 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -11,7 +11,7 @@ return `I am ${partner2}, and I don't like ${typeOfDance}ing with ${partner1}!`; }; console.log (dance ('Timothy', 'Elizabeth', 'waltz')); - + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ @@ -19,14 +19,25 @@ 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`! + let count = 0; // 2- Declare a function `counter`. It should increment and return `count`. + let counter = () => { + 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. + return counter; }; -// Example usage: const myCounter = counterMaker(); -// myCounter(); // 1 -// myCounter(); // 2 +//NO ERROR BUT NOT WORKING! +let myCounter = counterMaker(); +myCounter(); +myCounter(); +myCounter(); +// counterMaker(); +// counterMaker(); +//myCounter(); // 1 +//myCounter(); // 2 // ==== Challenge 3: Make `counterMaker` more sophisticated ==== // It should have a `limit` parameter. Any counters we make with `counterMaker` @@ -35,6 +46,18 @@ const counterMaker = () => { // ==== 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`. + return { + increment: function() { + counter++; + return counter; + }, + decrement: function() { + counter --; + return counter; + } + }; // `increment` should increment a counter variable in closure scope and return it. // `decrement` should decrement the counter variable and return it. }; + +console.log(counterFactory); \ No newline at end of file