diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..58223619e 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,21 +56,35 @@ 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(function(runner) { + return 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 = []; -console.log(allCaps); +runners.map(function (names){ + return allCaps.push(`${names.first_name.toUpperCase()} ${names.last_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 = []; -console.log(largeShirts); +runners.filter(function (size) { + if (size.shirt_size === "L") { + largeShirts.push(`${size.first_name} ${size.last_name}`) + } + +}) + 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(function (acc, total){ + return acc + total.donation + }, 0); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== @@ -78,6 +92,26 @@ console.log(ticketPriceTotal); // Problem 1 +//return everyones name and email +let nameAndEmail = [] +runners.forEach(function(emails){ + return nameAndEmail.push(`${emails.first_name} ${emails.last_name} : ${emails.email}`) +}) + +console.log(nameAndEmail); // Problem 2 +const bigSpenders = runners.filter((mula) =>{ + return mula.donation > 250; + }); + + console.log(bigSpenders) +// Problem 3 -// Problem 3 \ No newline at end of file +const longestName = runners.reduce(function (a, b) { + if (a.first_name > b.first_name ) { + return a.first_name.length + } else { + return b.first_name.length + } + } ) + console.log(longestName) \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..85190f963 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -23,30 +23,48 @@ 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(leng) { + console.log(len); +}); + function last(arr, cb) { - // last passes the last item of the array into the callback. + return cb(arr[3]) } +last(items, function(end){ + console.log(end) +}) + function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x + y) } +sumNums(67, 90, function(add){ + console.log(add) +}) function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x * y)// multiplyNums multiplies two numbers and passes the result to the callback. } +multiplyNums(39, 5, function(multiply){ + console.log(multiply); +}) + + function contains(item, list, cb) { + return cb(item === list) +} +contains('Pencil', items, function (x){ + console.log(x) +} ) // contains checks if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. -} + /* STRETCH PROBLEM */ -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. -} + + diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..2d16b70aa 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,14 +1,27 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +function howOld (){ + const age = 27; + console.log(`I am ${age}`) + function ageAndBirth (){ + const dob = 'July 25, 1991'; + console.log(`I am ${age} and was born on {dob}`) + } +} + +howOld(); // ==== Challenge 2: Create a counter function ==== -const counter = () => { - // Return a function that when invoked increments and returns a counter variable. -}; -// Example usage: const newCounter = counter(); -// newCounter(); // 1 -// newCounter(); // 2 +const counter = (function(count) { + + return function() { + return count += 1; + } + }(0)); + + counter(); + /* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */