diff --git a/array_methods.js b/array_methods.js index 03d9fb9..d310ba1 100644 --- a/array_methods.js +++ b/array_methods.js @@ -54,20 +54,165 @@ 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 = []; -console.log(fullName); +let fullName = [{"id":1,"first_name":"Charmain","last_name":"Seiler"}, +{"id":2,"first_name":"Whitaker","last_name":"Ierland"}, +{"id":3,"first_name":"Julieta","last_name":"McCloid"}, +{"id":4,"first_name":"Martynne","last_name":"Paye"}, +{"id":5,"first_name":"Gussy","last_name":"Raraty"}, +{"id":6,"first_name":"Yule","last_name":"Tommasetti"}, +{"id":7,"first_name":"Kathie","last_name":"Majury"}, +{"id":8,"first_name":"Tanner","last_name":"Branton"}, +{"id":9,"first_name":"Sarina","last_name":"Lasham"}, +{"id":10,"first_name":"Bertie","last_name":"Lonergan"}, +{"id":11,"first_name":"Trevor","last_name":"Studd"}, +{"id":12,"first_name":"Malachi","last_name":"Okeshott"}, +{"id":13,"first_name":"Berget","last_name":"Logsdail"}, +{"id":14,"first_name":"Loise","last_name":"Rivlin"}, +{"id":15,"first_name":"Christan","last_name":"Kendall"}, +{"id":16,"first_name":"Kayla","last_name":"Whitwam"}, +{"id":17,"first_name":"Heddie","last_name":"Heningam",}, +{"id":18,"first_name":"Mace","last_name":"Ballinger"}, +{"id":19,"first_name":"Nola","last_name":"Abberley"}, +{"id":20,"first_name":"Nadine","last_name":"Tresler"}, +{"id":21,"first_name":"Ulrikaumeko","last_name":"Vuittet"}, +{"id":22,"first_name":"Saunder","last_name":"Spennock"}, +{"id":23,"first_name":"Carmel","last_name":"Woffinden"}, +{"id":24,"first_name":"Marielle","last_name":"Kimmel"}, +{"id":25,"first_name":"Brucie","last_name":"Burris"}, +{"id":26,"first_name":"Juan","last_name":"Berzon"}, +{"id":27,"first_name":"Sacha","last_name":"Olsen"}, +{"id":28,"first_name":"Jamey","last_name":"O'Nolan",}, +{"id":29,"first_name":"Morrie","last_name":"Rainard"}, +{"id":30,"first_name":"Fidel","last_name":"Roskelly"}, +{"id":31,"first_name":"Toni","last_name":"MacSweeney"}, +{"id":32,"first_name":"Jessey","last_name":"Walhedd"}, +{"id":33,"first_name":"Karola","last_name":"Piper"}, +{"id":34,"first_name":"Marley","last_name":"Mitchenson"}, +{"id":35,"first_name":"Marrilee","last_name":"Thrasher"}, +{"id":36,"first_name":"Tye","last_name":"Manie"}, +{"id":37,"first_name":"Charleen","last_name":"Sheering"}, +{"id":38,"first_name":"Valma","last_name":"Eynaud"}, +{"id":39,"first_name":"Dollie","last_name":"McDarmid"}, +{"id":40,"first_name":"Minna","last_name":"Hymas"}, +{"id":41,"first_name":"Jsandye","last_name":"Frend"}, +{"id":42,"first_name":"Yevette","last_name":"Hacket"}, +{"id":43,"first_name":"Hank","last_name":"Zebedee"}, +{"id":44,"first_name":"Jodie","last_name":"Stawell"}, +{"id":45,"first_name":"Falito","last_name":"Karsh"}, +{"id":46,"first_name":"Reginauld","last_name":"Purselowe"}, +{"id":47,"first_name":"Vida","last_name":"Tydd"}, +{"id":48,"first_name":"Anderea","last_name":"MacGiolla Pheadair"}, +{"id":49,"first_name":"Bel","last_name":"Alway"}, +{"id":50,"first_name":"Shell","last_name":"Baine"}] +fullName.forEach((item)=>{if(item.first_name ) { +console.log(fullName) +}}) +//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 = [ +{"id":2,"first_name":"Whitaker"}, +{"id":3,"first_name":"Julieta"}, +{"id":4,"first_name":"Martynne"}, +{"id":5,"first_name":"Gussy"}, +{"id":6,"first_name":"Yule"}, +{"id":7,"first_name":"Kathie"}, +{"id":8,"first_name":"Tanner"}, +{"id":9,"first_name":"Sarina"}, +{"id":10,"first_name":"Bertie"}, +{"id":11,"first_name":"Trevor"}, +{"id":12,"first_name":"Malachi"}, +{"id":13,"first_name":"Berget"}, +{"id":14,"first_name":"Loise"}, +{"id":15,"first_name":"Christan"}, +{"id":16,"first_name":"Kayla"}, +{"id":17,"first_name":"Heddie"}, +{"id":18,"first_name":"Mace"}, +{"id":19,"first_name":"Nola"}, +{"id":20,"first_name":"Nadine"}, +{"id":21,"first_name":"Ulrikaumeko"}, +{"id":22,"first_name":"Saunder"}, +{"id":23,"first_name":"Carmel"}, +{"id":24,"first_name":"Marielle"}, +{"id":25,"first_name":"Brucie"}, +{"id":26,"first_name":"Juan"}, +{"id":27,"first_name":"Sacha"}, +{"id":28,"first_name":"Jamey"}, +{"id":29,"first_name":"Morrie"}, +{"id":30,"first_name":"Fidel"}, +{"id":31,"first_name":"Toni"}, +{"id":32,"first_name":"Jessey"}, +{"id":33,"first_name":"Karola"}, +{"id":34,"first_name":"Marley"}, +{"id":35,"first_name":"Married"}, +{"id":36,"first_name":"Tye"}, +{"id":37,"first_name":"Charleen"}, +{"id":38,"first_name":"Valma"}, +{"id":39,"first_name":"Dollier"}, +{"id":40,"first_name":"Minna"}, +{"id":41,"first_name":"Jsandye"}, +{"id":42,"first_name":"Yevette"}, +{"id":43,"first_name":"Hank"}, +{"id":44,"first_name":"Jodie"}, +{"id":45,"first_name":"Falito"}, +{"id":46,"first_name":"Reginauld"}, +{"id":47,"first_name":"Vida"}, +{"id":48,"first_name":"Anderea"}, +{"id":49,"first_name":"Bel"}, +{"id":50,"first_name":"Shell"}] + + // ==== 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 + + console.log(allCaps); + let allcaps = runners.map(function(item){ + return item.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 = []; -console.log(largeShirts); + +let largeShirts = [{"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":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":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":14,"first_name":"Loise","last_name":"Rivlin","email":"lrivlind@behance.net","shirt_size":"2XL","company_name":"Linktype","donation":204}, +{"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":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":22,"first_name":"Saunder","last_name":"Spennock","email":"sspennockl@icq.com","shirt_size":"3XL","company_name":"Kwimbee","donation":213}, +{"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":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":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":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":46,"first_name":"Reginauld","last_name":"Purselowe","email":"rpurselowe19@thetimes.co.uk","shirt_size":"L","company_name":"Jabbersphere","donation":11}, +{"id":48,"first_name":"Anderea","last_name":"MacGiolla Pheadair","email":"amacgiollapheadair1b@xing.com","shirt_size":"2XL","company_name":"Kwimbee","donation":214}, +{"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}]; +const size = largeShirts.filter((item) => { + return item.shirt_size > large; +}) +console.log(size) + //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 variable and log the result +const totalDonation = runners.reduce((acc, ticketPriceTotal ) =>{ + return acc + ticketPriceTotal.donation +},0) +console.log(totalDonation) -console.log(ticketPriceTotal); \ No newline at end of file +//console.log(ticketPriceTotal); \ No newline at end of file diff --git a/callbacks.js b/callbacks.js index f9d9dd0..a767146 100644 --- a/callbacks.js +++ b/callbacks.js @@ -2,7 +2,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* + //Given this problem: function firstItem(arr, cb) { @@ -17,29 +17,60 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; firstItem(items, function(first) { console.log(first) }); -*/ -function getLength(arr, cb) { + +function getLength(arr, cb) { + return cb([arr.length -0]); // getLength passes the length of the array into the callback. } +getLength(items,function(one){console.log(one)}); + + function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[3]); } +last(items,function(last){ + console.log(last) +}); -function sumNums(x, y, cb) { +//function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. -} +//} +function sumNums( x ,y, cb) +{return cb( x , y);} + +function add(x, y) { + return x + y} +console.log(sumNums(2, 3, add)); + -function multiplyNums(x, y, cb) { +//function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. -} +//} + +//function multiplyNums( x , y, cb){ + //return cb(x, y); +//} +//function multiply(x, y){ + // return x * y} +//console.log(multiplyNums(5, 5, multiply)); This way works too, I used arrow function down + +function multiplyNums( x , y, cb){ + return cb(x, y);} +const multiply = (x, y)=> x * y +console.log(multiplyNums(2, 5, 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. -} + return cb (item, list)} +function things (Pencil, Notebook) {return true} + console.log(contains(pencil, Notebook, things)); /* STRETCH PROBLEM */