From 847b130eeb359ea3146cc84c4972be7cb1f9a92f Mon Sep 17 00:00:00 2001 From: Issac Moreno Date: Tue, 15 Oct 2019 15:00:11 -0700 Subject: [PATCH] Finished MVP --- assignments/array-methods.js | 11 +++++++---- assignments/callbacks.js | 17 +++++++++++++++++ assignments/closure.js | 5 ++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..45214289f 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,22 +57,25 @@ 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(runner => (runner.full_name = `${runner.first_name} ${runner.last_name}`)); +let fullNames = runners.map(runner => runner['full_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(runner => runner['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(runner => runner.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 donations = runners.map(runner => runner['donation']); +let ticketPriceTotal = donations.reduce((total, donation) => total + donation); + console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..e124df42d 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -41,25 +41,42 @@ 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 testLen = getLength(items, length=> `Array length is ${length}`); +console.log(testLen); function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length - 1]); } +const lastTest = last(items, item => `Last item is ${item}`); +console.log(lastTest); function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x+y); } +const sumOfNums = sumNums(6,8, sum => `Sum is ${sum}`); +console.log(sumOfNums); function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x*y); } +const mulSum = multiplyNums(4, 9, product => `Product is ${product}`); +console.log(mulSum); 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 check1 = contains('Notebook', items, includes => `Item included: ${includes}`); +console.log(check1); +const check2 = contains('Book', items, includes => `Item included ${includes}`); +console.log(check2); /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..3f1355ee6 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,7 +4,10 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. - +const closure = () => { + let closeData = ''; + return data => (closureData += data); +}; /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */