From 9af8eda7eefb71b583955461c281dcd009c1319c Mon Sep 17 00:00:00 2001 From: Jason Barr Date: Sat, 30 Mar 2019 11:35:22 -0500 Subject: [PATCH 1/6] Update checklist --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 From fb1741ec64bf6cec37785fb12a4ba995c452f799 Mon Sep 17 00:00:00 2001 From: Jason Barr Date: Sat, 30 Mar 2019 12:34:11 -0500 Subject: [PATCH 2/6] Do callbacks exercises --- assignments/callbacks.js | 68 ++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 13 deletions(-) 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))); } From d49c5fd2c3064d27df543090f77b3c3822e0a795 Mon Sep 17 00:00:00 2001 From: Jason Barr Date: Sat, 30 Mar 2019 12:39:07 -0500 Subject: [PATCH 3/6] Do array method exercises --- assignments/array-methods.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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 From 7156b8a6ad1e8e6d0f4d691ae09b99cac1923abb Mon Sep 17 00:00:00 2001 From: Jason Barr Date: Sat, 30 Mar 2019 12:51:08 -0500 Subject: [PATCH 4/6] Do closure basic exercise --- assignments/closure.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/assignments/closure.js b/assignments/closure.js index 4307524fc..357dd5c95 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,6 +1,16 @@ // ==== 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 */ From b1e8f79fcff2f37115b364dea7bcfad4f7fc3802 Mon Sep 17 00:00:00 2001 From: Jason Barr Date: Sat, 30 Mar 2019 12:58:22 -0500 Subject: [PATCH 5/6] Remove nonexistent file from script src --- assignments/index.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 + From 7fc7c83d4140d0e2b19274943788c37be743d594 Mon Sep 17 00:00:00 2001 From: Jason Barr Date: Sat, 30 Mar 2019 13:12:29 -0500 Subject: [PATCH 6/6] Closure stretch problems --- assignments/closure.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/assignments/closure.js b/assignments/closure.js index 357dd5c95..f39abf6f2 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -17,8 +17,12 @@ const pageHeader = h1('Welcome to my page'); // ==== 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 @@ -28,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, + }) };