diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..02b3aa345 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,29 +57,52 @@ 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 = []; +let fullNames = runners.map((runners) => { + return runners.first_name + ' ' + runners.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 = []; +let firstNamesAllCaps = runners.map((runners) => { + return runners.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 = []; +let runnersLargeSizeShirt = runners.filter((runners) => { + return runners.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; +let ticketPriceTotal = runners.reduce((total, runners) => { + return total += runners.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: Require emails and their last names for a congratulatory email after the race. -// Problem 2 +let mailingList = runners.map((runners) => { + return runners.email + " is the email of " + runners.last_name; +}); +console.log(mailingList); -// Problem 3 \ No newline at end of file +// Problem 2: DabZ, Livetube, and Quaxo accidentally all have the same colored shirts. Filter the runners who are from these companies so they can choose the right shirt instead of one of the different companies'. + +let redShirts = runners.filter((runners) => { + return runners.company_name === "DabZ" || runners.company_name === "Livetube" || runners.company_name === "Quaxo"; +}); +console.log(redShirts); + +// Problem 3: A special letter will be sent out to those who have donated more than 100$. Find out how many of the runners have donated more than 100$. + +let specialLetter = runners.reduce((overHundred, runners) => { + return overHundred += (runners.donation >= 100); +}, 0); +console.log(specialLetter); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..691008597 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,57 @@ 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); } +const test1 = getLength(items, length => { + return length +}); +console.log(test1); + function last(arr, cb) { // last passes the last item of the array into the callback. -} + return cb(arr[arr.length - 1]); +}; + +const test2 = last(items, item => `I love my ${item}!`); +console.log(test2); function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. -} + return cb(x + y); +}; + +const test3 = sumNums(10, 15, sum => { + return sum; +}); +console.log(test3); function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. -} + return cb(x * y); +}; + +const test4 = multiplyNums(565, 1000, product => { + return product; +}); +console.log(test4); 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)); +}; + +const test5 = contains('Pencil', items, cb => { + return cb +}); +console.log(test5); + +const test6 = contains('Eraser', items, cb => { + return cb +}); +console.log(test6); /* STRETCH PROBLEM */ @@ -66,4 +99,4 @@ 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. -} +}; diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..efa294c77 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,6 +4,16 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +const firstName = 'Anthony'; + +function voiceMail() { + const middleInitial = 'J.'; + const lastName = 'Crowley'; + console.log('The name is ' + firstName + ' ' + middleInitial + ' ' + lastName + '. Leave a message, and do it in style.'); +}; + +voiceMail(); + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */