diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..275b5f7da 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,29 +57,33 @@ 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(runner => (runner.full_name = `${runner.first_name} ${runner.last_name}`)); +let fullNames = runners.map(runner => runner['full_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 = []; +let firstNamesAllCaps = runners.map(runner => runner['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 = []; +let runnersLargeSizeShirt = runners.filter(runner => runner.shirt_size === 'L'); 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; +let donations = runners.map(runner => runner['donation']); +let ticketPriceTotal = donations.reduce((total, donation) => total + donation); 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 +// Samsumg changed their domain and now we need to correct all the emails for @samsung.com address to @sungsam.com +runners.forEach(runner => (runner.email = runner.email.replace('@samsung.com', '@sungsam.com'))); +console.log(runners.filter(runner => runner.email.includes('sungsam'))); // Problem 2 -// Problem 3 \ No newline at end of file + +// Problem 3 diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..ba03fee40 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -41,29 +41,61 @@ 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); } +const testLen1 = getLength(items, length => `Array Length is ${length}`); +console.log(testLen1); + + function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length - 1]); } +const testLast = last(items, item => `Last item is ${item}`); +console.log(testLast); //Gum + +//==================================================== function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x + y); } +const testSum = sumNums(1, 8, sum => `Sum is ${sum}`); +console.log(testSum); //9 + +//==================================================== function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(x * y); } +const testMult = multiplyNums(2, 8, product => `Product is ${product}`); +console.log(testMult); //Product is 16 + +//==================================================== 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)); } +const testContains = contains('Notebook', items, includes => `Item included: ${includes}`); +console.log(testContains); //Item included: true + +const testContains2 = contains('Book', items, includes => `Item included: ${includes}`); +console.log(testContains2); //Item included: 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. + return cb(array.filter((item, index) => array.indexOf(item) === index)); } + +const items_dup = [...items, ...items]; +const testRemoveDuplicates = removeDuplicates(items_dup, arr => console.log(arr)); +//['Pencil', 'Notebook', 'yo-yo', 'Gum' \ No newline at end of file diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..306ba8d6e 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,9 +4,16 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +const closure = () => { + let closureData = ''; + return data => (closureData += data); +}; -/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ +const mytestClosure = closure(); +console.log(mytestClosure('Hi!')); +console.log(mytestClosure(" ...here we are again hi")); +/* 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 = () => { @@ -16,18 +23,56 @@ const counterMaker = () => { // 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. + let count = 1; + return () => { + return count++; + }; }; + // Example usage: const myCounter = counterMaker(); // myCounter(); // 1 // myCounter(); // 2 +console.log('Counter V1'); +const mycounter = counterMaker(); +console.log(`This should be 1: ${mycounter()}`); +console.log(`This should be 2: ${mycounter()}`); // ==== 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. +const counterMaker2 = (limit = 10) => { + let count = 1; + return () => { + return count > limit ? (count = 1) : count++; + }; +}; + +console.log('Counter V2'); +const counterV2 = counterMaker2(2); +console.log(`This should be 1: ${counterV2()}`); +console.log(`This should be 2: ${counterV2()}`); +console.log(`This should be 1: ${counterV2()}`); // ==== Challenge 4: Create a counter function with an object that can increment and decrement ==== -const counterFactory = () => { +const counterFunction = (limit = 10) => { // 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 count = 0; + return { + increment: () => { + count = count >= limit ? 1 : count + 1; + return count; + }, + decrement: () => { + count = count >= limit ? 0 : count - 1; + return count; + }, + }; }; + +console.log('Counter V3'); +const counterV3 = counterFunction(); +console.log(`This should be 1: ${counterV3.increment()}`); +console.log(`This should be 2: ${counterV3.increment()}`); +console.log(`This should be 1: ${counterV3.decrement()}`); \ No newline at end of file