From 3f75f6629a2e46b28f017ae11752da2f881f9d26 Mon Sep 17 00:00:00 2001 From: SachinBenny Date: Sun, 31 Mar 2019 22:57:00 -0500 Subject: [PATCH 1/2] javascript 2 --- assignments/array-methods.js | 29 +++++++++++++++++++++++------ assignments/callbacks.js | 24 ++++++++++++++++++------ assignments/closure.js | 8 ++++++++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f692c35c6..e25994a15 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -54,30 +54,47 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c {"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}]; // ==== 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. +// 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(function(names){ + fullName.push(`${names.first_name} ${names.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 = []; -console.log(allCaps); +runners.forEach(function(names) +{ + allCaps.push(names.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 = []; +largeShirts = runners.filter(runner => runner.shirt_size ==="L") 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 = []; + +let ticketPriceTotal = runners.reduce((totaldonations, runner) => {return totaldonations += runner.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 - return details of people with donation over 100 +let highdonation =[] +highdonation = runners.filter(runner => runner.donation > 100) +console.log(highdonation) + -// Problem 2 +// Problem 2 array of all company names +let companyNames = [] +companyNames = runners.map((runner, index) => {return runner.company_name}) +console.log(companyNames) -// Problem 3 \ No newline at end of file +// Problem 3 diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c1c013800..64fdfb548 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,10 +2,10 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* +/* + + //Given this problem: - //Given this problem: - function firstItem(arr, cb) { // firstItem passes the first item of the given array to the callback function. } @@ -17,7 +17,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; return cb(arr[0]); } - // Function invocation + // Function invocation firstItem(items, function(first) { console.log(first) }); @@ -27,25 +27,37 @@ 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) } +getLength(items, function(length){ +console.log(length)}) + function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length-1]) } +last(items, function(lastitem){console.log(lastitem)}) function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x+y) } +sumNums(2,4, function sum(sumof){console.log(sumof)}) function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x*y) } +multiplyNums(4,2, function multiply(result){console.log(result)}) 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 (console.log(list.includes(item)) +) + } + contains('Pencil', items, function verify(result) {console.log(result)}) /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { diff --git a/assignments/closure.js b/assignments/closure.js index 4307524fc..d0691fb38 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,5 +1,13 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +const fn = "Sachin"; +function fullname() +{ + const ln = "Benny"; + console.log(`${fn} ${ln}`); +} +fullname(); + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ From af8cb69d5ba1c35dc4d97863dd784185bd8b2a71 Mon Sep 17 00:00:00 2001 From: SachinBenny Date: Sun, 7 Apr 2019 15:56:32 -0500 Subject: [PATCH 2/2] changes to array methods --- assignments/array-methods.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index e25994a15..aec785a8d 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -64,9 +64,9 @@ 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.forEach(function(names) +allCaps = runners.map(function(names) { - allCaps.push(names.first_name.toUpperCase()) + return names.first_name.toUpperCase() }) console.log(allCaps); @@ -97,4 +97,8 @@ let companyNames = [] companyNames = runners.map((runner, index) => {return runner.company_name}) console.log(companyNames) -// Problem 3 +// Problem 3 all company names with "inc." added to their name +let newCompanyNames = [] +newCompanyNames = runners.map(function(company_name){ +return (`${company_name} inc`) +})