diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..4cd90d840 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,28 +58,65 @@ 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(function(item){ + fullNames.push(`${item.first_name} ${item.last_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 = []; +// runners.forEach(function(item){ +// firstNamesAllCaps.push(item.first_name.toUpperCase()) +// }) + +firstNamesAllCaps = runners.map((item) => item.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 = []; + runnersLargeSizeShirt.push(runners.filter(item => item.shirt_size == "L")); //this wont work without the item => in the filter b/c you haven't given the filter anything to reference to create or look for item.shirt_size + + 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; + +// ticketPriceTotal = runners.reduce(function(acc, currentVal) {return acc += currentVal.donation}, 0); +// console.log(ticketPriceTotal); + +ticketPriceTotal = runners.reduce((acc, currentVal) => acc + currentVal.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: We want to give everyone who donated over 200 dollars a gift basket, get a list of those generous people + +let givingPeople = []; + +givingPeople = runners.filter(item => item.donation > 200); + +console.log(givingPeople); + + +// Problem 2: actually we want the tax to know exactly how much tax is coming from each donation, make an array that shows how much each person is paying to a 17% tax + +let taxMan = []; + +taxMan = runners.map((item) => item.donation * .17); + +console.log(taxMan); + +// Problem 3: We want to know how many people wore 2XL, reduce them to a number! -// Problem 2 +let totalMiddleWeights = 0; +let middleWeightsByName = []; +middleWeightsByName = runners.filter((item) => item.shirt_size == "2XL"); +console.log(middleWeightsByName); +totalMiddleWeights = middleWeightsByName.length; -// Problem 3 \ No newline at end of file +console.log(totalMiddleWeights); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..9815105e7 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -41,29 +41,98 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr); } +const arrLength = function(arr){ + return arr.length; +} + + +//////////////MY ATTEMPT at inline did not work//// +// function getLength(arr, arr => arr.length); +// const arrLength = arr => arr.length; + + +console.log(getLength(items, arrLength)); + + + + function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr); } +const arrLast = function(arr){ + return arr[arr.length -1]; +} + +console.log(last(items, arrLast)); + + + + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x, y); } +const addTwo = function(x,y){ + return x + y; +} + +console.log(sumNums(1, 2, addTwo)); + + + + + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x , y); +} + +const productTwo = function(x, y){ + return x * y; } +console.log(multiplyNums(3, 4, productTwo)); + + + 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(item, list); +} + +const runThroughList = function(item, list){ + return list.includes(item); + + } +console.log(contains('Pencil', items, runThroughList)); +console.log(contains('Eraser', items, runThroughList)); + + /* STRETCH PROBLEM */ +let testArray = [1, 1, 2, 4, 5, 6, 6, 7, 8] + 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. + + return cb(array); +} + +const removeSamsies = function(list){ + return list.filter(function(item, index){ + return list.indexOf(item) >= index; + }) } + +console.log(removeDuplicates(testArray, removeSamsies)); diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..e8bfd5b2e 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,12 +4,36 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +function spaceship(shipName){ + const position1 = "Navigator"; + const position2 = "Cook"; + + function lifepod(podNum){ + console.log(`${podNum} didn't have enough space for more than one person so that person is both the ${position1} and ${position2}.`) + }; + + lifepod("Pod5"); +} +spaceship("crazy horse"); + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ // ==== Challenge 2: Implement a "counter maker" function ==== const counterMaker = () => { + let count = 0; + + return function counter(){ //don't forget the damn RETURN + count += 1; + if (count >= 4){ + return count = count - 3; + }else{ + return count;} + }; + + + // IMPLEMENTATION OF counterMaker: // 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`! // 2- Declare a function `counter`. It should increment and return `count`. @@ -17,6 +41,13 @@ const counterMaker = () => { // "closes over" the `count` variable. It can "see" it in the parent scope! // 3- Return the `counter` function. }; + +const newCounter = counterMaker(); +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); + // Example usage: const myCounter = counterMaker(); // myCounter(); // 1 // myCounter(); // 2