diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f986e1ad8..0da5d8329 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,28 +56,101 @@ 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((runner) => { + fullName.push(`${runner.first_name} ${runner.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 = []; +runners.map((runner) => { + allCaps.push(runner.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 = []; +runners.filter((runner) => { + if (runner.shirt_size === 'L') { + return largeShirts.push(runner); + } +}) 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 totalDonation = runners.reduce((accum, value) => { + return accum + value.donation; +}, 0) +ticketPriceTotal.push(totalDonation) + 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 solve 3 unique problems using one or many of the array methods listed above. // Problem 1 +const allSize = { + 'S': 0, + 'M': 0, + 'L': 0, + 'XL': 0, + '2XL': 0, + '3XL' : 0 +}; + +const sizeCounter = (runner) => { + switch (runner.shirt_size) { + case 'S' : + allSize.S += 1; + break; + + case 'M' : + allSize.M += 1; + break; + + case 'L' : + allSize.L += 1; + break; + + case 'XL' : + allSize.XL += 1; + break; + + case '2XL' : + allSize["2XL"] += 1; + break; + + case '3XL' : + allSize["3XL"] += 1; + } +} + +runners.forEach(sizeCounter); +console.log(allSize); // Problem 2 +let organization = []; +const org = runners.filter(runner => { + if (runner.email.includes('.edu') || + runner.email.includes('.org')|| + runner.email.includes('.gov')) { + organization.push(runner); + } +}) + +console.log(organization) + +// Problem 3 + +const companies = []; + +const listComps = runners.forEach(runner => { + return companies.push(runner.company_name); +}) -// Problem 3 \ No newline at end of file +console.log(companies.sort()) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index a551f853b..a43ba3d1c 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,34 +1,53 @@ -const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; +const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum',]; 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. + return cb(arr[0]); } +firstItem(items, console.log); function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr.length); } +getLength(items, console.log) function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length - 1]); } +last(items, console.log) function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x + y); } +sumNums(5,6, console.log) function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x * y); } +multiplyNums(2,3, console.log) 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, console.log) /* STRETCH PROBLEM */ - +let dupArr = []; 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. + array.forEach(element => { + if (!dupArr.includes(element)) { + dupArr.push(element); + } + }); + return cb(dupArr); } +removeDuplicates(items, console.log) diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..4862a4959 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,14 +1,22 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! - - +const fullName = (firstName, lastName) => { + console.log(`My name is ${firstName} ${lastName}`); +} +fullName('Abdiel', 'Fernandez') // ==== Challenge 2: Create a counter function ==== +let count = 0; const counter = () => { // Return a function that when invoked increments and returns a counter variable. + return count += 1; }; +const newCounter = counter; // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 +console.log(newCounter()) +console.log(newCounter()) +console.log(newCounter()) /* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */ diff --git a/assignments/function-conversion.js b/assignments/function-conversion.js index 45685b25d..a744d3782 100644 --- a/assignments/function-conversion.js +++ b/assignments/function-conversion.js @@ -1,23 +1,38 @@ // 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 => { + return param; +} // let add = function (param1, param2) { // return param1 + param2; // }; // add(1,2); +let add = (param1, param2) => { + return param1 + param2; +}; +add(1,2); // let subtract = function (param1, param2) { // return param1 - param2; // }; // subtract(1,2); +let subract = (param1, param2) => { + return param1 - param2; +}; +subract(1,2); -// exampleArray = [1,2,3,4]; +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 => { + return num * 3; +}) +console.log(triple);