diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..80cc6e352 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,28 +56,56 @@ 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(item) { + fullName.push(`${item.first_name} ${item.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 = []; +const allCaps = runners.map(function(item) { + return item.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 = []; +const largeShirts = runners.filter(function(item) { + return item.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 = []; +const ticketPriceTotal = runners.reduce(function(total, item) { + return total + item.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 +// store emails in new array +let emails = []; +runners.forEach(function(item) { + emails.push(item.email); +}); +console.log(emails); // Problem 2 +// Check if first name includes the letter 'e'. Return list of runners with 'e' in name. +const eRunners = runners.filter(function(item) { + return (item.first_name.includes('e') || item.first_name.includes('E')); +}); +console.log(eRunners); -// Problem 3 \ No newline at end of file +// Problem 3 +// Multiply all donations by $100 to feel like we really raised some good money. +const bigMoney = runners.map(function(item) { + return item.donation * 100; +}); +const bigMoneyTotal = bigMoney.reduce(function(total, item) { + return total + item +}, 0); +console.log(bigMoneyTotal); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..933ec3438 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -24,29 +24,70 @@ 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(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(3,4, function(x, y) { + console.log(x+y); +}) + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x, y); } +multiplyNums(12, 13, function(x, y) { + console.log(x*y); +}) + 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)); } + +contains('Pencil', items, function(bool) { + console.log(bool); +}) + /* STRETCH PROBLEM */ +const dupArray = [1, 3, 5, 3, 8, 9, 10, 9, 20, 20, 30, 100]; +const uniques = []; 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. + uniques.push(array[0]); + for (let i = 1; i < array.length; i++) { + if (uniques.includes(array[i])) { + // do nothing + } else { + uniques.push(array[i]); + } + } + return cb(); } + +removeDuplicates(dupArray, function() { + console.log(uniques); +}) diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..66c1638a0 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,11 +1,28 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +function tellAge(age) { + const currentAge = age; + console.log(`I am ${age} years old`); + + function older() { + const newAge = 50; + console.log(`In ${newAge - age} years I will be ${newAge} years old`); + } + older(); +} + +tellAge(30); // ==== Challenge 2: Create a counter function ==== +let count = 0; const counter = () => { // Return a function that when invoked increments and returns a counter variable. + count++; + console.log(count); }; +counter(); +counter(); // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 @@ -13,8 +30,39 @@ const counter = () => { /* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */ // ==== Challenge 3: Create a counter function with an object that can increment and decrement ==== +let newCount = 0; +let myObj = {}; const counterFactory = () => { // Return an object that has two methods called `increment` and `decrement`. // `increment` should increment a counter variable in closure scope and return it. // `decrement` should decrement the counter variable and return it. -}; + myObj = {increment: function(){ + newCount++; + console.log(newCount); + } /*end of increment method */, + decrement: function(){ + newCount--; + console.log(newCount); + } // end of decrement method + } /* end of myObj */; + return myObj; +} + +counterFactory(); +myObj.increment(); +myObj.increment(); +myObj.increment(); +myObj.increment(); +myObj.increment(); +myObj.decrement(); +myObj.decrement(); +myObj.decrement(); +myObj.decrement(); +myObj.decrement(); + + + + + + +