From e4d64782275ba713536a4785e7089f90237bb7fc Mon Sep 17 00:00:00 2001 From: Braden Date: Tue, 15 Oct 2019 14:34:38 -0600 Subject: [PATCH 1/4] finished closure and callbacks --- assignments/callbacks.js | 71 +++++++++++++++++++++++++++++++++------- assignments/closure.js | 9 +++-- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..a8541b4c3 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,34 +1,34 @@ // Create a higher order function and invoke the callback 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']; +// 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. + firstItem passes the first item of the given array to the callback function. } - // SOLUTION: + SOLUTION: function firstItem(arr, cb) { return cb(arr[0]); } - // NOTES ON THE SOLUTION: + NOTES ON THE SOLUTION: - // firstItem is a higher order function. - // It expects a callback (referred to as `cb`) as its second argument. - // To test our solution, we can use the given `items` array and a variety of callbacks. - // Note how callbacks can be declared separately, or inlined. + firstItem is a higher order function. + It expects a callback (referred to as `cb`) as its second argument. + To test our solution, we can use the given `items` array and a variety of callbacks. + Note how callbacks can be declared separately, or inlined. - // TEST 1 (inlined callback): + TEST 1 (inlined callback): const test1 = firstItem(items, item => `I love my ${item}!`); console.log(test1); // "I love my Pencil!" - // TEST 2 (declaring callback before hand): + TEST 2 (declaring callback before hand): function logExorbitantPrice(article) { return `this ${article} is worth a million dollars!`; @@ -38,27 +38,74 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ +const items = ["Pencil", "Notebook", "yo-yo", "Gum"]; function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr); } +cbLength = arr => arr.length; + +//! console.log(getLength(items, cbLength)); + function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr); } +cbLast = arr => arr[arr.length - 1]; + +//! console.log(last(items, cbLast)); function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x, y); } +cbSumNums = (x, y) => x + y; + +//! console.log(sumNums(2, 4, cbSumNums)); function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x, y); } +cdMultiplyNums = (x, y) => x * y; +//! console.log(multiplyNums(3, 6, cdMultiplyNums)); + +// let items = ["Pencil", "Notebook", "yo-yo", "Gum"]; +/* 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. + list.forEach(function(currentValue) { + if (item == currentValue) { + return cb(true); + } else { + cb(false); + } + }); + //* return item == list[i] ? cb(true) : cb(false); } +//! let containsCb = argumentPassed => console.log(argumentPassed); + +//! contains("yo-yo", items, containsCb); +*/ + +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++) { + //* return item == list[i] ? cb(true) : cb(false); + if (item == list[i]) { + return cb(true); + } + } + return cb(false); +} +let containsCb = argumentPassed => console.log(argumentPassed); + +contains("yo-yo", items, containsCb); /* STRETCH PROBLEM */ @@ -67,3 +114,5 @@ function removeDuplicates(array, cb) { // Pass the duplicate free array to the callback function. // Do not mutate the original array. } + +// const items = ["Pencil", "Notebook", "yo-yo", "Gum"]; diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..3e7723182 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -3,11 +3,16 @@ // Keep it simple! Remember a closure is just a function // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. - +function gerald(param1, param2) { + let result = function() { + console.log(`${param1} is not ${param2}`); + }; + result(param1, param2); +} +gerald("Jackie", "George"); /* 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: From acdf04675cf7516eb501c0be249db0f64597bd52 Mon Sep 17 00:00:00 2001 From: Braden Date: Tue, 15 Oct 2019 16:56:24 -0600 Subject: [PATCH 2/4] completed assignment --- assignments/array-methods.js | 552 +++++++++++++++++++++++++++++++---- assignments/callbacks.js | 477 +++++++++++++++++++++++++++++- 2 files changed, 965 insertions(+), 64 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..5c526c1a9 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -3,83 +3,515 @@ // Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses. const runners = [ - { id: 1, first_name: "Charmain", last_name: "Seiler", email: "cseiler0@wired.com", shirt_size: "2XL", company_name: "Divanoodle", donation: 75 }, - { id: 2, first_name: "Whitaker", last_name: "Ierland", email: "wierland1@angelfire.com", shirt_size: "2XL", company_name: "Wordtune", donation: 148 }, - { id: 3, first_name: "Julieta", last_name: "McCloid", email: "jmccloid2@yahoo.com", shirt_size: "S", company_name: "Riffpedia", donation: 171 }, - { id: 4, first_name: "Martynne", last_name: "Paye", email: "mpaye3@sciencedaily.com", shirt_size: "XL", company_name: "Wordware", donation: 288 }, - { id: 5, first_name: "Gussy", last_name: "Raraty", email: "graraty4@ucoz.ru", shirt_size: "L", company_name: "Oozz", donation: 291 }, - { id: 6, first_name: "Yule", last_name: "Tommasetti", email: "ytommasetti5@state.gov", shirt_size: "S", company_name: "Yodo", donation: 27 }, - { id: 7, first_name: "Kathie", last_name: "Majury", email: "kmajury6@guardian.co.uk", shirt_size: "3XL", company_name: "Zoomcast", donation: 261 }, - { id: 8, first_name: "Tanner", last_name: "Branton", email: "tbranton7@tmall.com", shirt_size: "2XL", company_name: "Realmix", donation: 28 }, - { id: 9, first_name: "Sarina", last_name: "Lasham", email: "slasham8@toplist.cz", shirt_size: "XL", company_name: "Gigashots", donation: 110 }, - { id: 10, first_name: "Bertie", last_name: "Lonergan", email: "blonergan9@issuu.com", shirt_size: "3XL", company_name: "Skinte", donation: 62 }, - { id: 11, first_name: "Trevor", last_name: "Studd", email: "tstudda@networkadvertising.org", shirt_size: "S", company_name: "Cogidoo", donation: 76 }, - { id: 12, first_name: "Malachi", last_name: "Okeshott", email: "mokeshottb@chron.com", shirt_size: "M", company_name: "DabZ", donation: 91 }, - { id: 13, first_name: "Berget", last_name: "Logsdail", email: "blogsdailc@wix.com", shirt_size: "M", company_name: "Mymm", donation: 9 }, - { id: 14, first_name: "Loise", last_name: "Rivlin", email: "lrivlind@behance.net", shirt_size: "2XL", company_name: "Linktype", donation: 204 }, - { id: 15, first_name: "Christan", last_name: "Kendall", email: "ckendalle@example.com", shirt_size: "XS", company_name: "Skinix", donation: 252 }, - { id: 16, first_name: "Kayla", last_name: "Whitwam", email: "kwhitwamf@deliciousdays.com", shirt_size: "2XL", company_name: "Oyope", donation: 147 }, - { id: 17, first_name: "Heddie", last_name: "Heningam", email: "hheningamg@tripadvisor.com", shirt_size: "L", company_name: "Skinix", donation: 172 }, - { id: 18, first_name: "Mace", last_name: "Ballinger", email: "mballingerh@a8.net", shirt_size: "S", company_name: "Nlounge", donation: 266 }, - { id: 19, first_name: "Nola", last_name: "Abberley", email: "nabberleyi@jalbum.net", shirt_size: "XL", company_name: "Photospace", donation: 148 }, - { id: 20, first_name: "Nadine", last_name: "Tresler", email: "ntreslerj@marketwatch.com", shirt_size: "3XL", company_name: "Wikido", donation: 292 }, - { id: 21, first_name: "Ulrikaumeko", last_name: "Vuittet", email: "uvuittetk@gov.uk", shirt_size: "S", company_name: "Skinte", donation: 102 }, - { id: 22, first_name: "Saunder", last_name: "Spennock", email: "sspennockl@icq.com", shirt_size: "3XL", company_name: "Kwimbee", donation: 213 }, - { id: 23, first_name: "Carmel", last_name: "Woffinden", email: "cwoffindenm@wikispaces.com", shirt_size: "S", company_name: "Rooxo", donation: 137 }, - { id: 24, first_name: "Marielle", last_name: "Kimmel", email: "mkimmeln@jimdo.com", shirt_size: "M", company_name: "Livetube", donation: 96 }, - { id: 25, first_name: "Brucie", last_name: "Burris", email: "bburriso@slate.com", shirt_size: "2XL", company_name: "Wordtune", donation: 128 }, - { id: 26, first_name: "Juan", last_name: "Berzon", email: "jberzonp@soup.io", shirt_size: "3XL", company_name: "Einti", donation: 234 }, - { id: 27, first_name: "Sacha", last_name: "Olsen", email: "solsenq@reverbnation.com", shirt_size: "2XL", company_name: "Viva", donation: 190 }, - { id: 28, first_name: "Jamey", last_name: "O'Nolan", email: "jonolanr@samsung.com", shirt_size: "XL", company_name: "Skinix", donation: 31 }, - { id: 29, first_name: "Morrie", last_name: "Rainard", email: "mrainards@yale.edu", shirt_size: "XS", company_name: "Podcat", donation: 52 }, - { id: 30, first_name: "Fidel", last_name: "Roskelly", email: "froskellyt@ibm.com", shirt_size: "XS", company_name: "Avavee", donation: 5 }, - { id: 31, first_name: "Toni", last_name: "MacSweeney", email: "tmacsweeneyu@parallels.com", shirt_size: "M", company_name: "Jaloo", donation: 82 }, - { id: 32, first_name: "Jessey", last_name: "Walhedd", email: "jwalheddv@slashdot.org", shirt_size: "L", company_name: "Trilia", donation: 5 }, - { id: 33, first_name: "Karola", last_name: "Piper", email: "kpiperw@ucsd.edu", shirt_size: "3XL", company_name: "Yombu", donation: 110 }, - { id: 34, first_name: "Marley", last_name: "Mitchenson", email: "mmitchensonx@webeden.co.uk", shirt_size: "M", company_name: "Zoonoodle", donation: 97 }, - { id: 35, first_name: "Marrilee", last_name: "Thrasher", email: "mthrashery@opensource.org", shirt_size: "XL", company_name: "Bluejam", donation: 17 }, - { id: 36, first_name: "Tye", last_name: "Manie", email: "tmaniez@netscape.com", shirt_size: "L", company_name: "Kanoodle", donation: 30 }, - { id: 37, first_name: "Charleen", last_name: "Sheering", email: "csheering10@mit.edu", shirt_size: "3XL", company_name: "Jatri", donation: 262 }, - { id: 38, first_name: "Valma", last_name: "Eynaud", email: "veynaud11@archive.org", shirt_size: "XS", company_name: "Jaxbean", donation: 212 }, - { id: 39, first_name: "Dollie", last_name: "McDarmid", email: "dmcdarmid12@tinyurl.com", shirt_size: "S", company_name: "Kayveo", donation: 74 }, - { id: 40, first_name: "Minna", last_name: "Hymas", email: "mhymas13@cornell.edu", shirt_size: "XS", company_name: "Vimbo", donation: 101 }, - { id: 41, first_name: "Jsandye", last_name: "Frend", email: "jfrend14@ca.gov", shirt_size: "XS", company_name: "Latz", donation: 156 }, - { id: 42, first_name: "Yevette", last_name: "Hacket", email: "yhacket15@wp.com", shirt_size: "XL", company_name: "Lazzy", donation: 291 }, - { id: 43, first_name: "Hank", last_name: "Zebedee", email: "hzebedee16@ezinearticles.com", shirt_size: "L", company_name: "Gigashots", donation: 241 }, - { id: 44, first_name: "Jodie", last_name: "Stawell", email: "jstawell17@yale.edu", shirt_size: "S", company_name: "Jaxspan", donation: 262 }, - { id: 45, first_name: "Falito", last_name: "Karsh", email: "fkarsh18@pcworld.com", shirt_size: "S", company_name: "Mycat", donation: 239 }, - { id: 46, first_name: "Reginauld", last_name: "Purselowe", email: "rpurselowe19@thetimes.co.uk", shirt_size: "L", company_name: "Jabbersphere", donation: 11 }, - { id: 47, first_name: "Vida", last_name: "Tydd", email: "vtydd1a@dropbox.com", shirt_size: "S", company_name: "Quaxo", donation: 55 }, - { id: 48, first_name: "Anderea", last_name: "MacGiolla Pheadair", email: "amacgiollapheadair1b@xing.com", shirt_size: "2XL", company_name: "Kwimbee", donation: 214 }, - { id: 49, first_name: "Bel", last_name: "Alway", email: "balway1c@ow.ly", shirt_size: "S", company_name: "Voolia", donation: 107 }, - { id: 50, first_name: "Shell", last_name: "Baine", email: "sbaine1d@intel.com", shirt_size: "M", company_name: "Gabtype", donation: 171 }, + { + id: 1, + first_name: "Charmain", + last_name: "Seiler", + email: "cseiler0@wired.com", + shirt_size: "2XL", + company_name: "Divanoodle", + donation: 75 + }, + { + id: 2, + first_name: "Whitaker", + last_name: "Ierland", + email: "wierland1@angelfire.com", + shirt_size: "2XL", + company_name: "Wordtune", + donation: 148 + }, + { + id: 3, + first_name: "Julieta", + last_name: "McCloid", + email: "jmccloid2@yahoo.com", + shirt_size: "S", + company_name: "Riffpedia", + donation: 171 + }, + { + id: 4, + first_name: "Martynne", + last_name: "Paye", + email: "mpaye3@sciencedaily.com", + shirt_size: "XL", + company_name: "Wordware", + donation: 288 + }, + { + id: 5, + first_name: "Gussy", + last_name: "Raraty", + email: "graraty4@ucoz.ru", + shirt_size: "L", + company_name: "Oozz", + donation: 291 + }, + { + id: 6, + first_name: "Yule", + last_name: "Tommasetti", + email: "ytommasetti5@state.gov", + shirt_size: "S", + company_name: "Yodo", + donation: 27 + }, + { + id: 7, + first_name: "Kathie", + last_name: "Majury", + email: "kmajury6@guardian.co.uk", + shirt_size: "3XL", + company_name: "Zoomcast", + donation: 261 + }, + { + id: 8, + first_name: "Tanner", + last_name: "Branton", + email: "tbranton7@tmall.com", + shirt_size: "2XL", + company_name: "Realmix", + donation: 28 + }, + { + id: 9, + first_name: "Sarina", + last_name: "Lasham", + email: "slasham8@toplist.cz", + shirt_size: "XL", + company_name: "Gigashots", + donation: 110 + }, + { + id: 10, + first_name: "Bertie", + last_name: "Lonergan", + email: "blonergan9@issuu.com", + shirt_size: "3XL", + company_name: "Skinte", + donation: 62 + }, + { + id: 11, + first_name: "Trevor", + last_name: "Studd", + email: "tstudda@networkadvertising.org", + shirt_size: "S", + company_name: "Cogidoo", + donation: 76 + }, + { + id: 12, + first_name: "Malachi", + last_name: "Okeshott", + email: "mokeshottb@chron.com", + shirt_size: "M", + company_name: "DabZ", + donation: 91 + }, + { + id: 13, + first_name: "Berget", + last_name: "Logsdail", + email: "blogsdailc@wix.com", + shirt_size: "M", + company_name: "Mymm", + donation: 9 + }, + { + id: 14, + first_name: "Loise", + last_name: "Rivlin", + email: "lrivlind@behance.net", + shirt_size: "2XL", + company_name: "Linktype", + donation: 204 + }, + { + id: 15, + first_name: "Christan", + last_name: "Kendall", + email: "ckendalle@example.com", + shirt_size: "XS", + company_name: "Skinix", + donation: 252 + }, + { + id: 16, + first_name: "Kayla", + last_name: "Whitwam", + email: "kwhitwamf@deliciousdays.com", + shirt_size: "2XL", + company_name: "Oyope", + donation: 147 + }, + { + id: 17, + first_name: "Heddie", + last_name: "Heningam", + email: "hheningamg@tripadvisor.com", + shirt_size: "L", + company_name: "Skinix", + donation: 172 + }, + { + id: 18, + first_name: "Mace", + last_name: "Ballinger", + email: "mballingerh@a8.net", + shirt_size: "S", + company_name: "Nlounge", + donation: 266 + }, + { + id: 19, + first_name: "Nola", + last_name: "Abberley", + email: "nabberleyi@jalbum.net", + shirt_size: "XL", + company_name: "Photospace", + donation: 148 + }, + { + id: 20, + first_name: "Nadine", + last_name: "Tresler", + email: "ntreslerj@marketwatch.com", + shirt_size: "3XL", + company_name: "Wikido", + donation: 292 + }, + { + id: 21, + first_name: "Ulrikaumeko", + last_name: "Vuittet", + email: "uvuittetk@gov.uk", + shirt_size: "S", + company_name: "Skinte", + donation: 102 + }, + { + id: 22, + first_name: "Saunder", + last_name: "Spennock", + email: "sspennockl@icq.com", + shirt_size: "3XL", + company_name: "Kwimbee", + donation: 213 + }, + { + id: 23, + first_name: "Carmel", + last_name: "Woffinden", + email: "cwoffindenm@wikispaces.com", + shirt_size: "S", + company_name: "Rooxo", + donation: 137 + }, + { + id: 24, + first_name: "Marielle", + last_name: "Kimmel", + email: "mkimmeln@jimdo.com", + shirt_size: "M", + company_name: "Livetube", + donation: 96 + }, + { + id: 25, + first_name: "Brucie", + last_name: "Burris", + email: "bburriso@slate.com", + shirt_size: "2XL", + company_name: "Wordtune", + donation: 128 + }, + { + id: 26, + first_name: "Juan", + last_name: "Berzon", + email: "jberzonp@soup.io", + shirt_size: "3XL", + company_name: "Einti", + donation: 234 + }, + { + id: 27, + first_name: "Sacha", + last_name: "Olsen", + email: "solsenq@reverbnation.com", + shirt_size: "2XL", + company_name: "Viva", + donation: 190 + }, + { + id: 28, + first_name: "Jamey", + last_name: "O'Nolan", + email: "jonolanr@samsung.com", + shirt_size: "XL", + company_name: "Skinix", + donation: 31 + }, + { + id: 29, + first_name: "Morrie", + last_name: "Rainard", + email: "mrainards@yale.edu", + shirt_size: "XS", + company_name: "Podcat", + donation: 52 + }, + { + id: 30, + first_name: "Fidel", + last_name: "Roskelly", + email: "froskellyt@ibm.com", + shirt_size: "XS", + company_name: "Avavee", + donation: 5 + }, + { + id: 31, + first_name: "Toni", + last_name: "MacSweeney", + email: "tmacsweeneyu@parallels.com", + shirt_size: "M", + company_name: "Jaloo", + donation: 82 + }, + { + id: 32, + first_name: "Jessey", + last_name: "Walhedd", + email: "jwalheddv@slashdot.org", + shirt_size: "L", + company_name: "Trilia", + donation: 5 + }, + { + id: 33, + first_name: "Karola", + last_name: "Piper", + email: "kpiperw@ucsd.edu", + shirt_size: "3XL", + company_name: "Yombu", + donation: 110 + }, + { + id: 34, + first_name: "Marley", + last_name: "Mitchenson", + email: "mmitchensonx@webeden.co.uk", + shirt_size: "M", + company_name: "Zoonoodle", + donation: 97 + }, + { + id: 35, + first_name: "Marrilee", + last_name: "Thrasher", + email: "mthrashery@opensource.org", + shirt_size: "XL", + company_name: "Bluejam", + donation: 17 + }, + { + id: 36, + first_name: "Tye", + last_name: "Manie", + email: "tmaniez@netscape.com", + shirt_size: "L", + company_name: "Kanoodle", + donation: 30 + }, + { + id: 37, + first_name: "Charleen", + last_name: "Sheering", + email: "csheering10@mit.edu", + shirt_size: "3XL", + company_name: "Jatri", + donation: 262 + }, + { + id: 38, + first_name: "Valma", + last_name: "Eynaud", + email: "veynaud11@archive.org", + shirt_size: "XS", + company_name: "Jaxbean", + donation: 212 + }, + { + id: 39, + first_name: "Dollie", + last_name: "McDarmid", + email: "dmcdarmid12@tinyurl.com", + shirt_size: "S", + company_name: "Kayveo", + donation: 74 + }, + { + id: 40, + first_name: "Minna", + last_name: "Hymas", + email: "mhymas13@cornell.edu", + shirt_size: "XS", + company_name: "Vimbo", + donation: 101 + }, + { + id: 41, + first_name: "Jsandye", + last_name: "Frend", + email: "jfrend14@ca.gov", + shirt_size: "XS", + company_name: "Latz", + donation: 156 + }, + { + id: 42, + first_name: "Yevette", + last_name: "Hacket", + email: "yhacket15@wp.com", + shirt_size: "XL", + company_name: "Lazzy", + donation: 291 + }, + { + id: 43, + first_name: "Hank", + last_name: "Zebedee", + email: "hzebedee16@ezinearticles.com", + shirt_size: "L", + company_name: "Gigashots", + donation: 241 + }, + { + id: 44, + first_name: "Jodie", + last_name: "Stawell", + email: "jstawell17@yale.edu", + shirt_size: "S", + company_name: "Jaxspan", + donation: 262 + }, + { + id: 45, + first_name: "Falito", + last_name: "Karsh", + email: "fkarsh18@pcworld.com", + shirt_size: "S", + company_name: "Mycat", + donation: 239 + }, + { + id: 46, + first_name: "Reginauld", + last_name: "Purselowe", + email: "rpurselowe19@thetimes.co.uk", + shirt_size: "L", + company_name: "Jabbersphere", + donation: 11 + }, + { + id: 47, + first_name: "Vida", + last_name: "Tydd", + email: "vtydd1a@dropbox.com", + shirt_size: "S", + company_name: "Quaxo", + donation: 55 + }, + { + id: 48, + first_name: "Anderea", + last_name: "MacGiolla Pheadair", + email: "amacgiollapheadair1b@xing.com", + shirt_size: "2XL", + company_name: "Kwimbee", + donation: 214 + }, + { + id: 49, + first_name: "Bel", + last_name: "Alway", + email: "balway1c@ow.ly", + shirt_size: "S", + company_name: "Voolia", + donation: 107 + }, + { + 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 and populate a new array called `fullNames`. This array will contain just strings. let fullNames = []; -console.log(fullNames); +//* runners.forEach(list = (arr) => fullNames.push(`${arr.id} ${arr.first_name} ${arr.last_name}`)) +runners.forEach(function(arr) { + fullNames.unshift(`${arr.first_name}`); +}); +// runners.forEach(cbFunction); +// const cbFunction = arr => +// fullNames.push(`${arr.id} ${arr.first_name} ${arr.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 = []; -console.log(firstNamesAllCaps); +//! let firstNamesAllCaps = runners.map(arr => `${arr.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 = []; -console.log(runnersLargeSizeShirt); +//! let runnersLargeSizeShirt = runners.filter(arr => arr["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; -console.log(ticketPriceTotal); +let ticketPriceTotal = runners.reduce(function(accumulator, arr) { + return accumulator + arr.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 +// Problem 1 -- donations higher than $250 +highestDoners = runners.filter(function(runner) { + if (runner.donation > 250) { + return `${runner.first_name} donated ${runner.donation}`; + } +}); +//! console.log(highestDoners); + +// Problem 2 -- need a list of first name runners with letters 5 or less all caps +shortNameRunners = []; +runners.filter(function(runner) { + if (runner.first_name.length < 6) { + shortNameRunners.push(runner.first_name.toUpperCase()); + } +}); +console.log(shortNameRunners); +shortNameRunnersSorted = shortNameRunners.sort(); +console.log(shortNameRunnersSorted); + +// Problem 3 -- make a list of all the emails -// Problem 2 +runnersEmail = runners.map(runner => runner.email); -// Problem 3 \ No newline at end of file +console.log(runnersEmail); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index a8541b4c3..1d6c4eb0a 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -103,16 +103,485 @@ function contains(item, list, cb) { } return cb(false); } -let containsCb = argumentPassed => console.log(argumentPassed); +//! let containsCb = argumentPassed => console.log(argumentPassed); + +//! contains("yo-yo", items, containsCb); -contains("yo-yo", items, containsCb); +const runners = [ + { + id: 1, + first_name: "Charmain", + last_name: "Seiler", + email: "cseiler0@wired.com", + shirt_size: "2XL", + company_name: "Divanoodle", + donation: 75 + }, + { + id: 2, + first_name: "Whitaker", + last_name: "Ierland", + email: "wierland1@angelfire.com", + shirt_size: "2XL", + company_name: "Wordtune", + donation: 148 + }, + { + id: 3, + first_name: "Julieta", + last_name: "McCloid", + email: "jmccloid2@yahoo.com", + shirt_size: "S", + company_name: "Riffpedia", + donation: 171 + }, + { + id: 4, + first_name: "Martynne", + last_name: "Paye", + email: "mpaye3@sciencedaily.com", + shirt_size: "XL", + company_name: "Wordware", + donation: 288 + }, + { + id: 5, + first_name: "Gussy", + last_name: "Raraty", + email: "graraty4@ucoz.ru", + shirt_size: "L", + company_name: "Oozz", + donation: 291 + }, + { + id: 6, + first_name: "Yule", + last_name: "Tommasetti", + email: "ytommasetti5@state.gov", + shirt_size: "S", + company_name: "Yodo", + donation: 27 + }, + { + id: 7, + first_name: "Kathie", + last_name: "Majury", + email: "kmajury6@guardian.co.uk", + shirt_size: "3XL", + company_name: "Zoomcast", + donation: 261 + }, + { + id: 8, + first_name: "Tanner", + last_name: "Branton", + email: "tbranton7@tmall.com", + shirt_size: "2XL", + company_name: "Realmix", + donation: 28 + }, + { + id: 9, + first_name: "Sarina", + last_name: "Lasham", + email: "slasham8@toplist.cz", + shirt_size: "XL", + company_name: "Gigashots", + donation: 110 + }, + { + id: 10, + first_name: "Bertie", + last_name: "Lonergan", + email: "blonergan9@issuu.com", + shirt_size: "3XL", + company_name: "Skinte", + donation: 62 + }, + { + id: 11, + first_name: "Trevor", + last_name: "Studd", + email: "tstudda@networkadvertising.org", + shirt_size: "S", + company_name: "Cogidoo", + donation: 76 + }, + { + id: 12, + first_name: "Malachi", + last_name: "Okeshott", + email: "mokeshottb@chron.com", + shirt_size: "M", + company_name: "DabZ", + donation: 91 + }, + { + id: 13, + first_name: "Berget", + last_name: "Logsdail", + email: "blogsdailc@wix.com", + shirt_size: "M", + company_name: "Mymm", + donation: 9 + }, + { + id: 14, + first_name: "Loise", + last_name: "Rivlin", + email: "lrivlind@behance.net", + shirt_size: "2XL", + company_name: "Linktype", + donation: 204 + }, + { + id: 15, + first_name: "Christan", + last_name: "Kendall", + email: "ckendalle@example.com", + shirt_size: "XS", + company_name: "Skinix", + donation: 252 + }, + { + id: 16, + first_name: "Kayla", + last_name: "Whitwam", + email: "kwhitwamf@deliciousdays.com", + shirt_size: "2XL", + company_name: "Oyope", + donation: 147 + }, + { + id: 17, + first_name: "Heddie", + last_name: "Heningam", + email: "hheningamg@tripadvisor.com", + shirt_size: "L", + company_name: "Skinix", + donation: 172 + }, + { + id: 18, + first_name: "Mace", + last_name: "Ballinger", + email: "mballingerh@a8.net", + shirt_size: "S", + company_name: "Nlounge", + donation: 266 + }, + { + id: 19, + first_name: "Nola", + last_name: "Abberley", + email: "nabberleyi@jalbum.net", + shirt_size: "XL", + company_name: "Photospace", + donation: 148 + }, + { + id: 20, + first_name: "Nadine", + last_name: "Tresler", + email: "ntreslerj@marketwatch.com", + shirt_size: "3XL", + company_name: "Wikido", + donation: 292 + }, + { + id: 21, + first_name: "Ulrikaumeko", + last_name: "Vuittet", + email: "uvuittetk@gov.uk", + shirt_size: "S", + company_name: "Skinte", + donation: 102 + }, + { + id: 22, + first_name: "Saunder", + last_name: "Spennock", + email: "sspennockl@icq.com", + shirt_size: "3XL", + company_name: "Kwimbee", + donation: 213 + }, + { + id: 23, + first_name: "Carmel", + last_name: "Woffinden", + email: "cwoffindenm@wikispaces.com", + shirt_size: "S", + company_name: "Rooxo", + donation: 137 + }, + { + id: 24, + first_name: "Marielle", + last_name: "Kimmel", + email: "mkimmeln@jimdo.com", + shirt_size: "M", + company_name: "Livetube", + donation: 96 + }, + { + id: 25, + first_name: "Brucie", + last_name: "Burris", + email: "bburriso@slate.com", + shirt_size: "2XL", + company_name: "Wordtune", + donation: 128 + }, + { + id: 26, + first_name: "Juan", + last_name: "Berzon", + email: "jberzonp@soup.io", + shirt_size: "3XL", + company_name: "Einti", + donation: 234 + }, + { + id: 27, + first_name: "Sacha", + last_name: "Olsen", + email: "solsenq@reverbnation.com", + shirt_size: "2XL", + company_name: "Viva", + donation: 190 + }, + { + id: 28, + first_name: "Jamey", + last_name: "O'Nolan", + email: "jonolanr@samsung.com", + shirt_size: "XL", + company_name: "Skinix", + donation: 31 + }, + { + id: 29, + first_name: "Morrie", + last_name: "Rainard", + email: "mrainards@yale.edu", + shirt_size: "XS", + company_name: "Podcat", + donation: 52 + }, + { + id: 30, + first_name: "Fidel", + last_name: "Roskelly", + email: "froskellyt@ibm.com", + shirt_size: "XS", + company_name: "Avavee", + donation: 5 + }, + { + id: 31, + first_name: "Toni", + last_name: "MacSweeney", + email: "tmacsweeneyu@parallels.com", + shirt_size: "M", + company_name: "Jaloo", + donation: 82 + }, + { + id: 32, + first_name: "Jessey", + last_name: "Walhedd", + email: "jwalheddv@slashdot.org", + shirt_size: "L", + company_name: "Trilia", + donation: 5 + }, + { + id: 33, + first_name: "Karola", + last_name: "Piper", + email: "kpiperw@ucsd.edu", + shirt_size: "3XL", + company_name: "Yombu", + donation: 110 + }, + { + id: 34, + first_name: "Marley", + last_name: "Mitchenson", + email: "mmitchensonx@webeden.co.uk", + shirt_size: "M", + company_name: "Zoonoodle", + donation: 97 + }, + { + id: 35, + first_name: "Marrilee", + last_name: "Thrasher", + email: "mthrashery@opensource.org", + shirt_size: "XL", + company_name: "Bluejam", + donation: 17 + }, + { + id: 36, + first_name: "Tye", + last_name: "Manie", + email: "tmaniez@netscape.com", + shirt_size: "L", + company_name: "Kanoodle", + donation: 30 + }, + { + id: 37, + first_name: "Charleen", + last_name: "Sheering", + email: "csheering10@mit.edu", + shirt_size: "3XL", + company_name: "Jatri", + donation: 262 + }, + { + id: 38, + first_name: "Valma", + last_name: "Eynaud", + email: "veynaud11@archive.org", + shirt_size: "XS", + company_name: "Jaxbean", + donation: 212 + }, + { + id: 39, + first_name: "Dollie", + last_name: "McDarmid", + email: "dmcdarmid12@tinyurl.com", + shirt_size: "S", + company_name: "Kayveo", + donation: 74 + }, + { + id: 40, + first_name: "Minna", + last_name: "Hymas", + email: "mhymas13@cornell.edu", + shirt_size: "XS", + company_name: "Vimbo", + donation: 101 + }, + { + id: 41, + first_name: "Jsandye", + last_name: "Frend", + email: "jfrend14@ca.gov", + shirt_size: "XS", + company_name: "Latz", + donation: 156 + }, + { + id: 42, + first_name: "Hanky", //todo CHANGE NAME HERE ***************************** + last_name: "Hacket", + email: "yhacket15@wp.com", + shirt_size: "XL", + company_name: "Lazzy", + donation: 291 + }, + { + id: 43, + first_name: "Vida", //todo ********************** + last_name: "Zebedee", + email: "hzebedee16@ezinearticles.com", + shirt_size: "L", + company_name: "Gigashots", + donation: 241 + }, + { + id: 44, + first_name: "Jodie", + last_name: "Stawell", + email: "jstawell17@yale.edu", + shirt_size: "S", + company_name: "Jaxspan", + donation: 262 + }, + { + id: 45, + first_name: "Vidaa", + last_name: "Karsh", + email: "fkarsh18@pcworld.com", + shirt_size: "S", + company_name: "Mycat", + donation: 239 + }, + { + id: 46, + first_name: "Vitality", + last_name: "Purselowe", + email: "rpurselowe19@thetimes.co.uk", + shirt_size: "L", + company_name: "Jabbersphere", + donation: 11 + }, + { + id: 47, + first_name: "Vida", + last_name: "Tydd", + email: "vtydd1a@dropbox.com", + shirt_size: "S", + company_name: "Quaxo", + donation: 55 + }, + { + id: 48, + first_name: "Shelll", + last_name: "MacGiolla Pheadair", + email: "amacgiollapheadair1b@xing.com", + shirt_size: "2XL", + company_name: "Kwimbee", + donation: 214 + }, + { + id: 49, + first_name: "Sheld", + last_name: "Alway", + email: "balway1c@ow.ly", + shirt_size: "S", + company_name: "Voolia", + donation: 107 + }, + { + id: 50, + first_name: "She", + last_name: "Baine", + email: "sbaine1d@intel.com", + shirt_size: "M", + company_name: "Gabtype", + donation: 171 + } +]; -/* STRETCH PROBLEM */ +//todo 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. + for (let i = 0; i < array.length; i++) { + let j = i + 1; + if (j < array.length) { + do { + if (array[i].first_name === array[j].first_name) { + return cb( + `${array[i].first_name} and ${array[j].first_name} are duplicates` + ); + } + j++; + } while (j < array.length); + } + } + return cb("No Duplicates Found"); } -// const items = ["Pencil", "Notebook", "yo-yo", "Gum"]; +removeDuplicatesCb = argument => console.log(argument); + +removeDuplicates(runners, removeDuplicatesCb); From 00a2264ddb5799730bde5b0092ad85f3aee95c38 Mon Sep 17 00:00:00 2001 From: Braden Date: Wed, 16 Oct 2019 15:37:46 -0600 Subject: [PATCH 3/4] Added more details and did a Stretch --- assignments/callbacks.js | 11 ++++------- assignments/closure.js | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 1d6c4eb0a..f2def83ab 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -71,7 +71,7 @@ function multiplyNums(x, y, cb) { } cdMultiplyNums = (x, y) => x * y; -//! console.log(multiplyNums(3, 6, cdMultiplyNums)); +// ! console.log(multiplyNums(3, 6, cdMultiplyNums)); // let items = ["Pencil", "Notebook", "yo-yo", "Gum"]; /* @@ -562,10 +562,7 @@ const runners = [ //todo 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. +function findDuplicates(array, cb) { for (let i = 0; i < array.length; i++) { let j = i + 1; if (j < array.length) { @@ -582,6 +579,6 @@ function removeDuplicates(array, cb) { return cb("No Duplicates Found"); } -removeDuplicatesCb = argument => console.log(argument); +findDuplicatesCb = argument => console.log(argument); -removeDuplicates(runners, removeDuplicatesCb); +findDuplicates(runners, findDuplicatesCb); diff --git a/assignments/closure.js b/assignments/closure.js index 3e7723182..b1a063742 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -15,6 +15,7 @@ gerald("Jackie", "George"); // ==== Challenge 2: Implement a "counter maker" function ==== const counterMaker = () => { + //todo add variable as // 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`. From f32f9b6a78b81df261814d493c3dabac8cbe3660 Mon Sep 17 00:00:00 2001 From: Braden Date: Fri, 7 Feb 2020 13:10:22 -0700 Subject: [PATCH 4/4] commit --- assignments/array-methods.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 5c526c1a9..62ada916e 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -476,7 +476,7 @@ runners.forEach(function(arr) { // ==== 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(arr => arr["shirt_size"] == "L"); +! let runnersLargeSizeShirt = runners.filter(arr => arr["shirt_size"] == "L"); //! console.log(runnersLargeSizeShirt);