From 211075d5492d3825cbf0ed6ffa6b9041457a7122 Mon Sep 17 00:00:00 2001 From: toneiojimz Date: Tue, 15 Oct 2019 17:30:41 -0700 Subject: [PATCH] updated --- assignments/array-methods.js | 74 ++++++++++++++++++++++++++++++++++-- assignments/callbacks.js | 51 ++++++++++++++++++++++--- assignments/closure.js | 34 ++++++++++++++--- 3 files changed, 144 insertions(+), 15 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..343d93638 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -58,28 +58,96 @@ 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(runners){ + return fullNames.push(`${runners.first_name} ${runners.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.map((runners, index) => {firstNamesAllCaps[index] = runners.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 = runners.filter(runner => { + if (runner.shirt_size === 'L'){ + return runner; + } +}); 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((totalValue, runner) => { + return totalValue + 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, create an array called nameId to include first name and id numbers. + +let nameId = []; + +runners.forEach(function(runners){ + return nameId.push(`${runners.first_name} , Id: ${runners.id}`)}); + +console.log(nameId); + + + +// Problem 2, create an array called sXs for small and extra small sized shirts + +let sXs = []; + +sXs = runners.filter(runner => { + if ((runner.shirt_size === 'XS')|| (runner.shirt_size === 'S')){ + return runner; + } +}); +console.log(sXs); + + + + +// Problem 3 create an array called highRollers to include donations of more than 100 + +let highRollers = []; -// Problem 2 +highRollers = runners.filter(runner => { + if (runner.donation > 100){ + return runner; + } +}); -// Problem 3 \ No newline at end of file +console.log(highRollers); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..161ead1b1 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -40,30 +40,69 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { - // getLength passes the length of the array into the callback. + /* getLength passes the length of the array into the callback.*/ + return cb(arr.length); } +getLength(items, function(arrLength){ + console.log(arrLength); +}); + + + 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(3,3, 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(3,3, 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. + if(list.includes(item)){ + return cb(true); +} else { + + return cb(false); } +}; + +contains('Gum', items, function(result){ + console.log(result); +}); /* 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. -} +//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); +//}// diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..1ae649089 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,5 +1,21 @@ // ==== Challenge 1: Write your own closure ==== // Write a closure of your own creation. + +function sayHello(greet) { + const yourGreeting = greet; + const name = 'Pat'; + console.log(`${yourGreeting}. How's everything?`); + + function answer() { + const myAnswer = "I'm ok, thanks"; + console.log(`${yourGreeting} ${name} ${myAnswer}`); + } + + answer(); +} + +console.log(sayHello('Hola!')); + // Keep it simple! Remember a closure is just a function // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. @@ -9,25 +25,31 @@ // ==== Challenge 2: Implement a "counter maker" function ==== + const counterMaker = () => { - // IMPLEMENTATION OF counterMaker: + // IMPLEMENTATION OF counterMaker:// + let count = 0; // 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`! + function counter(num){ + return num++; + } // 2- Declare a function `counter`. It should increment and return `count`. // NOTE: This `counter` function, being nested inside `counterMaker`, // "closes over" the `count` variable. It can "see" it in the parent scope! // 3- Return the `counter` function. + return counter(); }; -// Example usage: const myCounter = counterMaker(); -// myCounter(); // 1 -// myCounter(); // 2 +// Example usage: const myCounter = counter(); +console.log(counter()); // 1 +console.log(counter()); // 2 // ==== Challenge 3: Make `counterMaker` more sophisticated ==== // It should have a `limit` parameter. Any counters we make with `counterMaker` // will refuse to go over the limit, and start back at 1. // ==== Challenge 4: Create a counter function with an object that can increment and decrement ==== -const counterFactory = () => { +// 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. -}; +// };