diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f986e1ad8..1190b05f9 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,28 +56,64 @@ 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 = []; + +runners.forEach(x => fullName.push(x.first_name.concat(" " + x.last_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(x => x.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(x => x.shirt_size === "L"); + console.log(largeShirts); // ==== 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.map(x => x.donation).reduce((total, amount) => { + return total + amount; +}); 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 solve 3 unique problems using one or many of the array methods listed above. // Problem 1 +//Total donation amount who wears small shirts + +let smallShirts = []; + +smallShirtsTotal = runners.filter(x => x.shirt_size === "S").reduce((total, amount) => { + return total + amount.donation; +}, 0); +console.log(smallShirtsTotal); // Problem 2 +//Find who wears larger than "L" size shirts. + +let largerThanL = []; + +largerThanL = runners.filter(x => x.shirt_size === "XL" || x.shirt_size === "2XL" || x.shirt_size === "3XL" ); + +console.log(largerThanL); + +// Problem 3 +//Average donation of peeps wearing "M" shirts + +let averageDonation = []; -// Problem 3 \ No newline at end of file +mediumShirts = runners.filter(x => x.shirt_size === "M"); +averageDonation = mediumShirts.reduce((total, amount) => { + return (total += amount.donation) / mediumShirts.length; +}, 0); +console.log (averageDonation); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index a551f853b..5fd3ffa43 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,34 +1,81 @@ -const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; +const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Pencil', 'Notebook', 'yo-yo', 'Gum']; function firstItem(arr, cb) { // firstItem passes the first item of the given array to the callback function. + cb(arr[0]); } +firstItem(items, (firstItem) => { + console.log(firstItem); +}) + function getLength(arr, cb) { // getLength passes the length of the array into the callback. + cb(arr.length); } +getLength(items, (length) => { + console.log(length); +}) + function last(arr, cb) { // last passes the last item of the array into the callback. + cb(arr[arr.length - 1]); } +last(items, (theLast) => { + console.log(theLast); +}) + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + cb(x + y); } +sumNums(1, 2, (numSum) => { + console.log(numSum); +}) + + + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x * y); } +multiplyNums(2, 3, (multi) => { + console.log(multi); +}) + 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. + for (let i = 0; i < list.length; i++) { + if (list[i] === item) { + cb(true); + } + } + cb(false); } +contains('Gum', items, (contains) => { + console.log(contains); +}) + /* 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. + const unique = []; + for (let i = 0; i < array.length; i++) { + contains(array[i], unique, isUnique => isUnique ? undefined : unique.push(array[i])); + } + + cb(unique); } + +removeDuplicates(items, (duplicatesRemoved) => { + console.log(duplicatesRemoved); +}) \ No newline at end of file diff --git a/assignments/closure.js b/assignments/closure.js index 4037b64c9..2f0b269fd 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,19 +1,69 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! - +const square = (x) => { + return x * x; +} // ==== Challenge 2: Create a counter function ==== -const counter = () => { + // Return a function that when invoked increments and returns a counter variable. + +const counter = () => { + + let count = 0; + function counterIncrement () { + return ++count; + } + return counterIncrement; }; + +const newCounter = counter(); + +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); +console.log(newCounter()); + + + // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 +console.log(`----------------------------------------------------`); +console.log(`----------------------------------------------------`); +console.log(`----------------------------------------------------`); +console.log(`----------------------------------------------------`); +console.log(`----------------------------------------------------`); + // ==== 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 param = 0; + + return { + increment: function increment() { + return ++param; + }, + decrement: function decrement () { + return --param; + } + } +}; +const test = counterFactory(); + +console.log(`------ Increment starts here --------`); +console.log(test.increment()); +console.log(test.increment()); +console.log(test.increment()); +console.log(test.increment()); + +console.log(`-------- Decrement starts here ---------------------------`); +console.log(test.decrement()); +console.log(test.decrement()); +console.log(test.decrement()); diff --git a/assignments/function-conversion.js b/assignments/function-conversion.js index 5e6a658a4..597e7be7a 100644 --- a/assignments/function-conversion.js +++ b/assignments/function-conversion.js @@ -2,22 +2,44 @@ // let myFunction = function () {}; +let myFunction = () => { +}; + // let anotherFunction = function (param) { // return param; // }; +let anotherFunction = (param) => { + return param; +}; + // let add = function (param1, param2) { // return param1 + param2; // }; // add(1,2); +let add = (param1, param2) => { + return param1 + param2; +}; +console.log(add(1, 2)); -let subtract = function (param1, param2) { +//let subtract = function (param1, param2) { + //return param1 - param2; +//}; +//subtract(1,2); + +let subtract = (param1, param2) => { return param1 - param2; }; -subtract(1,2); //? +console.log(subtract(1, 2)); -exampleArray = [1,2,3,4]; +//exampleArray = [1,2,3,4]; // const triple = exampleArray.map(function (num) { // return num * 3; // }); -// console.log(triple); \ No newline at end of file +// console.log(triple); + +exampleArray = [1,2,3,4]; +const triple = exampleArray.map(x = (num) => { + return num * 3; + }); + console.log(triple);