diff --git a/README.md b/README.md index 6288977c2..7d992641a 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,10 @@ With some basic JavaScript principles in hand, we can now expand our skills out **Follow these steps to set up and work on your project:** -* [ ] Create a forked copy of this project. -* [ ] Add your project manager as collaborator on Github. -* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!). -* [ ] Create a new branch: git checkout -b ``. +* [x] Create a forked copy of this project. +* [x] Add your project manager as collaborator on Github. +* [x] Clone your OWN version of the repository (Not Lambda's by mistake!). +* [x] Create a new branch: git checkout -b ``. * [ ] Implement the project on your newly created `` branch, committing changes regularly. * [ ] Push commits: git push origin ``. @@ -50,4 +50,4 @@ We have learned that closures allow us to access values in scope that have alrea ## Stretch Goals * [ ] Go back through the stretch problems that you skipped over and complete as many as you can. -* [ ] Look up what an IIFE is in JavaScript and experiment with them \ No newline at end of file +* [ ] Look up what an IIFE is in JavaScript and experiment with them diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f692c35c6..ae889ed08 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,21 +56,27 @@ 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(runner => fullName.push(`${runner.first_name} ${runner.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 = fullName.map(name => { + let [first, last] = name.split(' '); + return `${first.toUpperCase()} ${last}` + +}); 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 = []; +let largeShirts = runners.filter(runner => runner.shirt_size !== 'XL'); 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 = []; +let ticketPriceTotal = runners.reduce((total, runner) => total + runner.donation, 0); console.log(ticketPriceTotal); // ==== Challenge 5: Be Creative ==== @@ -80,4 +86,4 @@ console.log(ticketPriceTotal); // Problem 2 -// Problem 3 \ No newline at end of file +// Problem 3 diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c1c013800..10cdc7a68 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,6 +2,25 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; +/** + * Recursive implementation of Array.prototype.map + * + * @param {Function} fn Callback to transform array elements + * @param {Array} param1 Array destructured into head and ...tail + * @return {Array} Transformed array + */ +const recursiveMap = function (fn, [head, ...tail]) { + if (head === undefined && tail.length < 1) { + return []; + } + + return [fn(head), ...recursiveMap(fn, tail)]; +} + +console.log(recursiveMap(function(str) { + return str.toLowerCase(); +}, items)); + /* //Given this problem: @@ -18,38 +37,61 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; } // Function invocation - firstItem(items, function(first) { - console.log(first) - }); + + */ + +/** + * Takes a Strings and returns the first letter + * + * @param {String} str a string + * @returns {String} first letter + */ +const firstLetter = function (str) { + return str[0]; +} -*/ +// Process first item in array +function firstItem(items, fn) { + return fn(items[0]); +} +// Get first letter of first in an array of strings +console.log(firstItem(items, firstLetter)); function getLength(arr, cb) { - // getLength passes the length of the array into the callback. + return cb(arr.length); } function last(arr, cb) { - // last passes the last item of the array into the callback. + return cb(arr[arr.length - 1]); } function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb(x + y); } function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. + return cb(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. + return cb(list.includes(item)); } /* 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 removed = []; + for (let i = 0; i < array.length; i++) { + if (!removed.includes(array[i])) { + removed.push(array[i]); + } + } + + return removed; +} + +// Alternatively +function removeDuplicatesV2(array, fn) { + return fn(Array.from(new Set(array))); } diff --git a/assignments/closure.js b/assignments/closure.js index 4307524fc..f39abf6f2 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,14 +1,28 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +const createHTMLElementFactory = function(elem) { + return function(text) { + const htmlElem = document.createElement(elem); + htmlElem.textContent = text; // undefined if not set with arg is the desired behavior + return htmlElem; + }; +} +// example usage +const h1 = createHTMLElementFactory('h1'); +const pageHeader = h1('Welcome to my page'); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ // ==== Challenge 2: Create a counter function ==== const counter = () => { - // Return a function that when invoked increments and returns a counter variable. + let counterVar = 0; + return function() { + return ++counterVar; // append increment operator so it increments then returns + } }; + // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 @@ -18,4 +32,9 @@ 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 i = 1; + return Object.assign({ + increment: () => ++i, + decrement: () => --i, + }) }; diff --git a/assignments/index.html b/assignments/index.html index d6c2e0f4d..69e95d591 100644 --- a/assignments/index.html +++ b/assignments/index.html @@ -10,10 +10,9 @@ -

JS II - Check your work in the console!

- \ No newline at end of file +