diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f692c35c6..ae1fb0953 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,28 +56,121 @@ 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 = []; +//i did the the old way for testing purposes +// for(let i = 0; i < runners.length; i++){ +// fullName.push(`${runners[i].first_name} ${runners[i].last_name}`); +// } +// console.log(fullName); + +runners.forEach(function(names){ + + fullName.push(`${names.first_name} ${names.last_name}`) + //console.log(value.first_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 = []; +allCaps = runners.map(function(names) { + return 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(shirt => shirt.shirt_size === 'L') + console.log(largeShirts); -// ==== Challenge 4: Use .reduce() ==== +// // ==== 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 = []; + +ticketPriceTotal = runners.reduce(function(ticket,item){ + +return ticket += 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 +// Problem 1: Find which company donated the most and output: companyName donationValue. +let maxVal = 0; +let biggestCompany = []; + +runners.forEach(function(value){ + if(value.donation > maxVal){ + maxVal = value.donation; + } +}); + +runners.filter(function(value){ + if(value.donation === maxVal){ + console.log(`${value.company_name} ${maxVal}`); + return; + } +}); + + +// Problem 2: if donations are over 50 dollars then gather the emails of the individuals to send a special thank you. +let runnerMail = []; +let runnerabv50 = []; +runnerabv50 = runners.filter(function(value){ + return value.donation > 50; +}); + + +runnerabv50.forEach(function(value){ + + runnerMail.push(`${value.email}`) + +}); +console.log(runnerMail) + +// Find out who wears a size small in the list of runners. +let sizeSmall = []; + +runners.filter((runner) => { + + if(runner.shirt_size === "S"){ + + sizeSmall.push(runner.first_name + " " + runner.last_name); + } +}); +console.log(sizeSmall); // Problem 2 +//The t-shirt supplier needs to know how many of each kind of shirt they will need to ship. Make an array for each size. +let sCount = []; +sCount = runners.filter((runner) =>{ + return runner.shirt_size === 'S'; +}) +let mCount = []; +mCount = runners.filter((runner) =>{ + return runner.shirt_size === 'M'; +}) +let lCount = []; +lCount = runners.filter((runner) =>{ + return runner.shirt_size === 'L'; +}) +let xlCount = []; +xlCount = runners.filter((runner) =>{ + return runner.shirt_size === 'XL'; +}) +let xxlCount = []; +xxlCount = runners.filter((runner) =>{ + return runner.shirt_size === '2XL'; +}) +let xxxlCount = []; +xxxlCount = runners.filter((runner) =>{ + return runner.shirt_size === '3XL'; +}) + +console.log(`We will need ${sCount.length} small, ${mCount.length} medium, ${lCount.length} large, ${xlCount.length} xl, ${xxlCount.length} xxl, and ${xxxlCount.length} 3xl shirts.`); -// Problem 3 \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c1c013800..944e19284 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,54 +2,101 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* //Given this problem: - function firstItem(arr, cb) { - // firstItem passes the first item of the given array to the callback function. - } - + // Potential Solution: - // Higher order function using "cb" as the call back function firstItem(arr, cb) { return cb(arr[0]); } - // Function invocation firstItem(items, function(first) { console.log(first) }); -*/ -function getLength(arr, cb) { - // getLength passes the length of the array into the callback. -} + function getLength(arr, cb) { + // getLength passes the length of the array into the callback. + } + function getLength(arr, cb){ + 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. +// cb(items.length[-1]); +// lastItem(items, cb function(last) { +// console.log(last); +// }); +// } -function last(arr, cb) { +function veryLast(arr, cb) { // last passes the last item of the array into the callback. + cb(items[items.length - 1]); } +veryLast(items, function(last) { + console.log(last); +}); + + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. } +function sumNums(x, y, cb){ + return cb(x + y); +} + +sumNums(5, 7, function(addition){ + console.log(addition) +}); + + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. } +function multiplyNums(x, y, cb) { + return cb(x * y); +} + +multiplyNums(2, 5, function(multiplication){ + console.log(multiplication) +}); + + 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. } +function contains(item, list, cb) { + if(list.includes(item)){ + return cb(true); + } + else{ + return cb(false); + } +} + +contains('yo-yo', items, function(trueFalse){ +console.log(trueFalse) +}); /* 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. + let newArray = array.map(item => {return item}); } + + diff --git a/assignments/closure.js b/assignments/closure.js index 4307524fc..90d513032 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,6 +1,12 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +function sayHello(name) { + const text = (`Hello ${name}`); + const say = function() { console.log(text); } + say(); +} +sayHello('Andy'); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ @@ -8,14 +14,30 @@ // ==== Challenge 2: Create a counter function ==== const counter = () => { // Return a function that when invoked increments and returns a counter variable. + let count = 0; + return(function() { return( ++count); }) }; +const newCounter = counter(); +console.log(newCounter()); +console.log(newCounter()); // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 + // ==== Challenge 3: Create a counter function with an object that can increment and decrement ==== 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. -}; + let counter = 0; + object = { + increment: function(){ + return counter++; + }, + decrement: function(){ + return counter--; + } + + } +}; \ No newline at end of file