diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..f4cd429df 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,28 +58,57 @@ 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 = []; + +runners.forEach(function(currentValue){ + let names = currentValue.first_name + ' ' + currentValue.last_name; + fullNames.push(names) +}); + 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(function(currentValue){ + return currentValue.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(function(currentValue){ + return currentValue.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(function(accumulator, currentValue){ + return accumulator + currentValue.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 Find out which which company is participating/donating to the 5k fun run. +let companiesDon = []; + +runners.forEach(function(currentValue){ + let companyName = currentValue.company_name; + companiesDon.push(companyName); +}); + +console.log(companiesDon); + +// Problem 2 edu +let eduRunners = runners.filter(function(currentValue){ + return currentValue.email === '.edu'; +}); +console.log(eduRunners); -// Problem 2 +// Problem 3 The organizer had already put down a huge dontaion before donations were being accepted. +let donationStart = runners.reduce(function(accumulator, currentValue){ + return accumulator + currentValue.donation; +}, 15000); -// Problem 3 \ No newline at end of file +console.log(donationStart); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..b54b9cd40 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -38,27 +38,48 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; console.log(test2); // "this Pencil is worth a million dollars!" */ - +// getLength passes the length of the array into the callback. function getLength(arr, cb) { - // getLength passes the length of the array into the callback. + return cb(items.length); } +getLength (items, lengthOfItems => { + console.log(lengthOfItems); +}); + +// last passes the last item of the array into the callback. function last(arr, cb) { - // last passes the last item of the array into the callback. + return cb(items.slice(-1)); } +last (items, lastItem => { + console.log(lastItem); +}); + +// sumNums adds two numbers (x, y) and passes the result to the callback. function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x, y); +} +function add (x, y){ + return x + y; } +console.log(sumNums(5,10,add)); +// multiplyNums multiplies two numbers and passes the result to the callback. function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x, y); } +function multiply (x, y){ + return x * y; +} +console.log(multiplyNums(4,8,multiply)); -function contains(item, list, cb) { - // contains checks if an item is present inside of the given array/list. +// contains checks if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. +function contains(item, list, cb) { + return cb(list.includes(item)); } +contains ('Pencil', items, console.log); /* STRETCH PROBLEM */ diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..e9c4f5d33 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,6 +4,17 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +function makeThemMultiply(num1) { + return function(num2) { + return num1 * num2; + }; +} + +let firstNumber = makeThemMultiply(4); +let secondNumber = makeThemMultiply(2); + +console.log(firstNumber(10)); +console.log(secondNumber(15)); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ diff --git a/assignments/index.html b/assignments/index.html index 69e95d591..9486dacf4 100644 --- a/assignments/index.html +++ b/assignments/index.html @@ -11,7 +11,7 @@ - +

JS II - Check your work in the console!