diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..ab4a7691c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + + + + { + "type": "node", + "name": "vscode-jest-tests", + "request": "launch", + "args": [ + "--runInBand" + ], + "cwd": "${workspaceFolder}", + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "program": "${workspaceFolder}/node_modules/jest/bin/jest" + } + ] +} diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f3862361e..6bf96b502 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -57,29 +57,49 @@ 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 = []; -console.log(fullNames); +let fullName = []; +runners.forEach(runner => fullName.push( `${runner.first_name} ${ runner.last_name}`)); +console.log(fullName); // ==== 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 = []; +firstNamesAllCaps = runners.map( runners => 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 => 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 allPrices = 0; +allPrices = runners.map(runner => runner.donation); +ticketPriceTotal.push(allPrices.reduce((a, c) => a + c)); 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 +let allCompanies = runners.map(runner => runner.company_name); +let uniqueCompanies = Array.from(new Set(allCompanies)); +let uniqueCompanies2 = allCompanies.filter((co, index) => allCompanies.indexOf(co) >= index); +console.log(uniqueCompanies); // Problem 2 -// Problem 3 \ No newline at end of file +let allDonorAmtByLastName = runners.map(runner => `${runner.last_name}: $${ runner.donation }` ); +console.log(allDonorAmtByLastName); + +// Problem 3 +let dotCompCo = []; +dotCompCo = runners.filter(runner => runner.email.includes(`.com`)); +let allDotCompCoPrices = 0; +allDotCompCoPrices = dotCompCo.map(runner => runner.donation).reduce((a, c) => a + c); +let avgTicketPriceTotal = ticketPriceTotal/runners.length; +console.log(`Average donation price from.com emails ${avgDotCompCoPrices > avgTicketPriceTotal ? `is` : `is not`} more than the average donation price for all companies.`); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index cb72e70c9..ff81adb9f 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,7 +2,13 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* +function getLength(arr, cb) { + cb(arr.length); +}; + + + +/* // GIVEN THIS PROBLEM: @@ -40,30 +46,58 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { + cb(arr.length); // getLength passes the length of the array into the callback. -} +}; +getLength(items, function (lenny) { + console.log(lenny); +}); function last(arr, cb) { + cb(arr[arr.length - 1]); // last passes the last item of the array into the callback. -} +}; +last(items, function (showLast) { + console.log(showLast); +}); function sumNums(x, y, cb) { + cb(x + y); // sumNums adds two numbers (x, y) and passes the result to the callback. -} +}; +sumNums(2, 3, function (adder) { + console.log(adder); +}); function multiplyNums(x, y, cb) { + cb(x * y); // multiplyNums multiplies two numbers and passes the result to the callback. -} +}; +multiplyNums(2, 3, function (product) { + console.log(product); +}); function contains(item, list, cb) { + cb(list.includes(item) ? true : false); // contains checks if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. -} +}; +contains(`Notebook`, items, function (hit) { + console.log(hit); -/* STRETCH PROBLEM */ +}); +/* STRETCH PROBLEM */ +const itemsDoubled = [`Pencil`, `Notebook`, `yo-yo`, `Gum`, `Notebook`, `yo-yo`, `Gum`]; function removeDuplicates(array, cb) { + + let uniqueItems = Array.from(new Set(array)); + cb(uniqueItems) +}; +removeDuplicates(itemsDoubled, function (nonDupe) { + console.log(nonDupe) +}); // removeDuplicates removes all duplicate values from the given array. // Pass the duplicate free array to the callback function. // Do not mutate the original array. -} + diff --git a/assignments/closure.js b/assignments/closure.js index 4b399c098..546e43719 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -4,30 +4,78 @@ // that manipulates variables defined in the outer scope. // The outer scope can be a parent function, or the top level of the script. +function add(num1) { + return function (num2) { + return num1 + num2; + } +}; +let add5 = adder(5); +let add7 = adder(7); +console.log(add7); /* 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 = () => { - // 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`. - // 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. -}; -// Example usage: const myCounter = counterMaker(); -// myCounter(); // 1 -// myCounter(); // 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 = () => { - // 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. -}; + const counter = () => + { + let count = 0; + return function () + { + let count = 0; + return function () + { + return ++count; + } + }; + console.log(newCounter); + console.log(newCounter); + console.log(newCounter); + + // 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`. + // 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` funnction. + + // Example usage: const myCounter = counterMaker(); + // myCounter(); // 1 + // myCounter(); // 2 + + // ==== Challenge 3: Make `counterMaker` more sophisticated ==== + // It should have a `limit` parameter. Any counters we make with `counterMaker` + const counterMaker = counter(); + // newCounter(); //1 + // newCounter(); //2 + console.log(newCounter()); + console.log(newCounter()); + + // 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 = () => + // { + // // 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.c + // let count = 0; { + + + // return { + // increment: function () + // { + // return ++count; + + // }, + // decrement: function () + // { + // return --count; + // }, + // } + // } + // console.log(counterFactory.increment()); + // console.log(counterFactory.decrement); + // } +