diff --git a/README.md b/README.md index a28a9c5f6..5083b1f5e 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,46 @@ - # JavaScript - II -With some basic JavaScript principles in hand, we can now expand our skills out even further by exploring callback functions, array methods, and closure. Finish each task in order as the concepts build on one another. +With some basic JavaScript principles in hand, we can now expand our skills out even further by exploring callback functions, array methods, and closure. Finish each task in order as the concepts build on one another. ## Task 1: Set Up The Project With Git -* [ ] Fork the project into your GitHub user account -* [ ] Clone the forked project into a directory on your machine -* [ ] You are now ready to build this project with your preferred IDE -* [ ] To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console. - +- [ ] Fork the project into your GitHub user account +- [ ] Clone the forked project into a directory on your machine +- [ ] You are now ready to build this project with your preferred IDE +- [ ] To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console. ## Task 2: Callbacks This task focuses on getting practice with 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 callback functions. +- [ ] 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 callback functions. -* [ ] Write out each function using the `ES5` `function` keyword syntax. +- [ ] Write out each function using the `ES5` `function` keyword syntax. -* [ ] Solve the problems listed. Save the stretch problems until you have completed Tasks 1-4. +- [ ] Solve the problems listed. Save the stretch problems until you have completed Tasks 1-4. ## Task 3: 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. +- [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file. -* [ ] Complete the problems provided to you +- [ ] Complete the problems provided to you -* [ ] 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. +- [ ] 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 4: Closures -We have learned that closures allow us to access values in scope that have already been invoked. +We have learned that closures allow us to access values in scope that have already been invoked. **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. -* [ ] Solve the problems listed. Save the stretch problems until you have completed Tasks 1-4. -* [ ] Once you have completed this task please submit a pull request against the original fork. +- [ ] Review the contents of the [closure.js](assignments/closure.js) file. +- [ ] Solve the problems listed. Save the stretch problems until you have completed Tasks 1-4. +- [ ] Once you have completed this task please submit a pull request against the original fork. ## Stretch Goals -* [ ] Arrow Function Syntax - [Check out this awesome guide for ES6 arrow syntax](https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26). You will see more and more arrow functions as you progress deeper into JavaScript. Use the [stretch-function-conversion.js](assignments/function-conversion.js) file as a helper challenge to showcase some of the differences between ES5 and ES6 syntax. +- [ ] Arrow Function Syntax - [Check out this awesome guide for ES6 arrow syntax](https://medium.freecodecamp.org/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26). You will see more and more arrow functions as you progress deeper into JavaScript. Use the [stretch-function-conversion.js](assignments/function-conversion.js) file as a helper challenge to showcase some of the differences between ES5 and ES6 syntax. -* [ ] Look up what an IIFE is in JavaScript and experiment with them \ No newline at end of file +- [ ] Look up what an IIFE is in JavaScript and experiment with them diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..ba81810f9 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -2,75 +2,484 @@ // 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}]; +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 + } +]; // ==== 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) { + let name = item.first_name + " " + item.last_name; + fullName.push(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); +allCaps = runners.map(item => 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 = []; +largeShirts = runners.filter(item => 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 = []; +ticketPriceTotal = runners.reduce((total, item) => total + item.donation, 0); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..c9430bad3 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,13 +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. -const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; +const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Pencil']; -/* +/* //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. } // Potential Solution: @@ -18,35 +18,83 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; firstItem(items, function(first) { console.log(first) }); - */ -function getLength(arr, cb) { - // getLength passes the length of the array into the callback. -} -function last(arr, cb) { - // last passes the last item of the array into the callback. -} +// function getLength(arr, cb) { +// // getLength passes the length of the array into the callback. +// } + +// function getLength(arr, cb) { +// return cb(getLength.length); +// } + +// getLength(items, function(length) { +// console.log(items.length) +// }); + + +// function last(arr, cb) { +// // last passes the last item of the array into the callback. +// } + +// function last(arr, cb) { +// return cb(arr[3]); +// } + +// 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. + // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x, y); } +const add = function(x, y) { + console.log(x + y); +} + +sumNums(5, 4, add); + + function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. + // 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. +const multiply = function(x, y) { + console.log(x * y); } +multiplyNums(6, 3, multiply); + +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 < item.length; i++) { + if (item[i] === list) { + return cb(true); + } + } + return cb(false); +}; + +contains(items, 'yo-yo', function(arrItem) { + console.log(arrItem ? 'yo-yo is in the list' : 'yo-yo is not in the list'); +}); + + /* 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. + // 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 uniqueArray = array.filter(function(item, index, self) { + return index = self.indexOf(item); + }); + return uniqueArray; } +console.log(removeDuplicates(items)); \ No newline at end of file diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..0a07acef7 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,20 +1,39 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +function favSport(sportName) { + const sport = sportName; + console.log(`My favorite sport is ${sport}!`); + + function favPlayer() { + const player = 'Kyrie Irving'; + console.log(`${sport}, is my favorite sport, and ${player} is my favorite ${sport} player.`); + } + favPlayer(); +} +favSport('Basketball'); // ==== Challenge 2: Create a counter function ==== const counter = () => { - // Return a function that when invoked increments and returns a counter variable. + let count = 0; + + return () => (++count); + // Return a function that when invoked increments and returns a counter variable. }; -// Example usage: const newCounter = counter(); -// newCounter(); // 1 -// newCounter(); // 2 +// Example usage: +const newCounter = counter(); +console.log(newCounter()); // 1 +console.log(newCounter()); // 2 +console.log(newCounter()); // 3 +console.log(newCounter()); // 4 +console.log(newCounter()); // 5 + /* 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. -}; + // 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. +}; \ No newline at end of file diff --git a/assignments/stretch-function-conversion.js b/assignments/stretch-function-conversion.js index 45685b25d..882e538c4 100644 --- a/assignments/stretch-function-conversion.js +++ b/assignments/stretch-function-conversion.js @@ -1,23 +1,18 @@ // Take the commented ES5 syntax and convert it to ES6 arrow Syntax -// let myFunction = function () {}; +let myFunction = () => {}; -// let anotherFunction = function (param) { -// return param; -// }; +let anotherFunction = param => param; -// let add = function (param1, param2) { -// return param1 + param2; -// }; -// add(1,2); -// let subtract = function (param1, param2) { -// return param1 - param2; -// }; -// subtract(1,2); +let add = (param1, param2) => param1 + param2; + +add(1, 2); + +let subtract = (param1, param2) => param1 - param2; + +subtract(1, 2); // exampleArray = [1,2,3,4]; -// const triple = exampleArray.map(function (num) { -// return num * 3; -// }); -// console.log(triple); \ No newline at end of file +const triple = exampleArray.map((num) => num * 3); +console.log(triple); \ No newline at end of file