From 0940a4fe7c07d0dec80a3ae78ab47d4c927e2994 Mon Sep 17 00:00:00 2001 From: Daniel Weinman Date: Tue, 27 Nov 2018 16:04:40 -0500 Subject: [PATCH 1/5] completed callbacks.js problem set --- assignments/callbacks.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..64cdc6bd9 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -24,25 +24,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); } +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[arr.length - 1]); } +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(x, y); } +sumNums(3,4, 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(12, 13, 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. + return cb(list.includes(item)); } + +contains('Pencil', items, function(bool) { + console.log(bool); +}) + /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { From 5273172c46efcca38f62b530b6ab027eea44e6e2 Mon Sep 17 00:00:00 2001 From: Daniel Weinman Date: Tue, 27 Nov 2018 16:27:40 -0500 Subject: [PATCH 2/5] completed all challenges in array-methods.js --- assignments/array-methods.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..80cc6e352 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,28 +56,56 @@ 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 = []; +runners.forEach(function(item) { + fullName.push(`${item.first_name} ${item.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 = []; +const allCaps = runners.map(function(item) { + return item.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 = []; +const largeShirts = runners.filter(function(item) { + return item.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 = []; +const ticketPriceTotal = runners.reduce(function(total, item) { + return total + item.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. // Problem 1 +// store emails in new array +let emails = []; +runners.forEach(function(item) { + emails.push(item.email); +}); +console.log(emails); // Problem 2 +// Check if first name includes the letter 'e'. Return list of runners with 'e' in name. +const eRunners = runners.filter(function(item) { + return (item.first_name.includes('e') || item.first_name.includes('E')); +}); +console.log(eRunners); -// Problem 3 \ No newline at end of file +// Problem 3 +// Multiply all donations by $100 to feel like we really raised some good money. +const bigMoney = runners.map(function(item) { + return item.donation * 100; +}); +const bigMoneyTotal = bigMoney.reduce(function(total, item) { + return total + item +}, 0); +console.log(bigMoneyTotal); \ No newline at end of file From 795c7cc64c4891e19ed3a7b258a8e145ac56268d Mon Sep 17 00:00:00 2001 From: Daniel Weinman Date: Tue, 27 Nov 2018 16:44:26 -0500 Subject: [PATCH 3/5] completed all challenges in closure.js --- assignments/closure.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..476012775 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,11 +1,28 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +function tellAge(age) { + const currentAge = age; + console.log(`I am ${age} years old`); + + function older() { + const newAge = 50; + console.log(`In ${newAge - age} years I will be ${newAge} years old`); + } + older(); +} + +tellAge(30); // ==== Challenge 2: Create a counter function ==== +let count = 0; const counter = () => { // Return a function that when invoked increments and returns a counter variable. + count++; + console.log(count); }; +counter(); +counter(); // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 @@ -18,3 +35,8 @@ const counterFactory = () => { // `increment` should increment a counter variable in closure scope and return it. // `decrement` should decrement the counter variable and return it. }; +function newFunction() { + const count = 0; + console.log(count + 1); +} + From 44c7e1ddccb356707316cfff015f4a4bf0b55eec Mon Sep 17 00:00:00 2001 From: Daniel Weinman Date: Tue, 27 Nov 2018 16:57:16 -0500 Subject: [PATCH 4/5] completed stretch goal in callbacks.js --- assignments/callbacks.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 64cdc6bd9..933ec3438 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -70,9 +70,24 @@ contains('Pencil', items, function(bool) { }) /* STRETCH PROBLEM */ +const dupArray = [1, 3, 5, 3, 8, 9, 10, 9, 20, 20, 30, 100]; +const uniques = []; 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. + uniques.push(array[0]); + for (let i = 1; i < array.length; i++) { + if (uniques.includes(array[i])) { + // do nothing + } else { + uniques.push(array[i]); + } + } + return cb(); } + +removeDuplicates(dupArray, function() { + console.log(uniques); +}) From 07790c2af646754fd1796157d457a1dbee9dda76 Mon Sep 17 00:00:00 2001 From: Daniel Weinman Date: Tue, 27 Nov 2018 17:22:08 -0500 Subject: [PATCH 5/5] completed stretch goal in closure.js --- assignments/closure.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/assignments/closure.js b/assignments/closure.js index 476012775..66c1638a0 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -30,13 +30,39 @@ counter(); /* 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 ==== +let newCount = 0; +let myObj = {}; 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. -}; -function newFunction() { - const count = 0; - console.log(count + 1); + myObj = {increment: function(){ + newCount++; + console.log(newCount); + } /*end of increment method */, + decrement: function(){ + newCount--; + console.log(newCount); + } // end of decrement method + } /* end of myObj */; + return myObj; } +counterFactory(); +myObj.increment(); +myObj.increment(); +myObj.increment(); +myObj.increment(); +myObj.increment(); +myObj.decrement(); +myObj.decrement(); +myObj.decrement(); +myObj.decrement(); +myObj.decrement(); + + + + + + +