From 163102b2f7cc5928193856db3058989578f0ea3c Mon Sep 17 00:00:00 2001 From: Jessica Novak Date: Tue, 15 Oct 2019 15:43:17 -0700 Subject: [PATCH] final commit --- assignments/array-methods.js | 45 ++++++++++++++++++++++++++++++++---- assignments/callbacks.js | 26 ++++++++++++++++++++- assignments/closure.js | 6 +++++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..88dc81cee 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,29 +57,66 @@ 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 (runners) { + fullNames.push(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 = []; +runners.map(function (runners) { + firstNamesAllCaps.push(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 = []; +runners.filter(function (runners) { + if (runners.shirt_size === 'L') { + runnersLargeSizeShirt.push(runners.first_name + ' ' + runners.last_name) + } +}) 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 (total, runners) { + ticketPriceTotal.push(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 The minimum donation was set to $100. Make a list of everyone that needs to pay up for forefit. +let donationRequirement = []; +runners.filter(function (runners) { + if (runners.donation < 100) { + donationRequirement.push(runners.first_name + ' ' + runners.last_name, "amount owed:" + (100 - runners.donation)); + } +}) +console.log('pay up', donationRequirement); + +// Problem 2 the organizer needs a roll put together in aplhabetical order by last name +let aplhabeticalOrder = []; +runners.forEach(function (runners) { + aplhabeticalOrder.push(runners.last_name + ' ' + runners.first_name); + aplhabeticalOrder.sort(); +}) +console.log(aplhabeticalOrder); -// Problem 2 +// Problem 3 all of the companies need to be added next to the name of the runners in a single list +let runnerCompanyList = []; +runners.forEach(function (runners) { + runnerCompanyList.push(runners.last_name + ' ' + runners.first_name + ', ' + runners.company_name) +}) -// Problem 3 \ No newline at end of file +console.log(runnerCompanyList); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..83875c4a0 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -41,23 +41,47 @@ 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); } -function last(arr, cb) { +getLength(items, function(length){ + console.log(length); +}); + +function lastItem(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length - 1]); } +lastItem(items, function(last){ + console.log(last); +}); + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x + y); } +sumNums(4, 8, function(sum){ + console.log(sum); +}); + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x * y); } +multiplyNums(4, 8, function(product){ + console.log(product); +}); + 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 < list.length; i++) + if(list[i] === item) + cb(true); + cb(false); } /* STRETCH PROBLEM */ diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..c5ff1564a 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -3,6 +3,12 @@ // Keep it simple! Remember a closure is just a function // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +const firstLetter = 'A'; + const alphabet = function() { + const secondLetter = 'B'; + return (`${firstLetter} ${secondLetter}`); + }; +console.log(alphabet()); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */