From fac95ec9007742d3e66051d85daf5c191379cfa5 Mon Sep 17 00:00:00 2001 From: Joscelyn Date: Tue, 11 Jun 2019 14:11:50 -0400 Subject: [PATCH 1/7] finished closure question --- README.md | 16 ---------------- assignments/closure.js | 23 ++++++++++++++++++++++- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6288977c2..5901c59a5 100644 --- a/README.md +++ b/README.md @@ -3,22 +3,6 @@ With some basic JavaScript principles in hand, we can now expand our skills out even further by exploring callback functions, array methods, and closure. Finish each task in order as the concepts build on one another. -## Set Up The Project With Git - -**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 ``. -* [ ] Implement the project on your newly created `` branch, committing changes regularly. -* [ ] Push commits: git push origin ``. - -**Follow these steps for completing your project.** - -* [ ] Submit a Pull-Request to merge Branch into master (student's Repo). **Please don't merge your own pull request** -* [ ] Add your project manager as a reviewer on the pull-request -* [ ] Your project manager will count the project as complete by merging the branch back into master. ## Task 2: Higher Order Functions and Callbacks diff --git a/assignments/closure.js b/assignments/closure.js index 4307524fc..a78d8cdc9 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -2,6 +2,28 @@ // Write a simple closure of your own creation. Keep it simple! +function minusMaker(minusNumber) { + let result; + function minus(firstNumber) { + + return result = firstNumber - minusNumber; + } + + return minus; + +} + +const minus5 = minusMaker(5); + +console.log(minus5(7)); +console.log(minus5(59)); + + + + + + + /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */ @@ -17,5 +39,4 @@ const counter = () => { 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. }; From 06e97594718b07e3e1eddee3e185a3666fbf9452 Mon Sep 17 00:00:00 2001 From: Joscelyn Date: Tue, 11 Jun 2019 14:32:09 -0400 Subject: [PATCH 2/7] finished closure stretch question 1 --- assignments/closure.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/assignments/closure.js b/assignments/closure.js index a78d8cdc9..a87a7aa77 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -28,12 +28,17 @@ console.log(minus5(59)); // ==== Challenge 2: Create a counter function ==== -const counter = () => { - // Return a function that when invoked increments and returns a counter variable. +function counter() { + let number = 0; + function add1() { + return ++number; + + } + return add1; }; -// Example usage: const newCounter = counter(); -// newCounter(); // 1 -// newCounter(); // 2 +const newCounter = counter(); +console.log(newCounter()); // 1 +console.log(newCounter()); // 2 // ==== Challenge 3: Create a counter function with an object that can increment and decrement ==== const counterFactory = () => { From 0a325ef3ee8e56f82d7b029dbc5ec5b5321ddb66 Mon Sep 17 00:00:00 2001 From: Joscelyn Date: Tue, 11 Jun 2019 15:45:21 -0400 Subject: [PATCH 3/7] finished callback section --- assignments/callbacks.js | 86 ++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c1c013800..83c81af62 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,55 +1,91 @@ -// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems. +// Create a higher order function and invoke the callback function to test your work. +//You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems. const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* - //Given this problem: - - function firstItem(arr, cb) { - // firstItem passes the first item of the given array to the callback function. - } - // Potential Solution: +//Given this problem: - // Higher order function using "cb" as the call back - function firstItem(arr, cb) { - return cb(arr[0]); - } +//function firstItem(arr, cb) { +// firstItem passes the first item of the given array to the callback function. +// - // Function invocation - firstItem(items, function(first) { - console.log(first) - }); +// Potential Solution: + +// Higher order function using "cb" as the call back + +const pets = ['cat', 'dog', 'mouse', 'bird', 'mouse']; + + + +function firstItem(arr, cb) { + return cb(arr[0]); +} + +// Function invocation +firstItem(pets, function (first) { + console.log(first) +}); -*/ function getLength(arr, cb) { - // getLength passes the length of the array into the callback. + cb(arr.length); } +getLength(pets, (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(pets, (last) => { + console.log(last); +}) + + function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. + cb(x + y); } +sumNums(5, 10, function (total) { + console.log(total); +}) + + function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x * y); } +multiplyNums(5, 6, function (product) { + console.log(product); +}) + + function contains(item, list, cb) { + cb(list.indexOf(item) !== -1); // 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('bird', pets, function (boolean) { + console.log(boolean); +}) + + + /* 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. +function removeDuplicates(array, cb) { /* got help from https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array */ + cb(uniqueArray = array.filter(function (item, pos, self) { + return self.indexOf(item) === pos; + })) } + +removeDuplicates(pets, function(uniqueArr) { + console.log(uniqueArr); +}) \ No newline at end of file From 68c6c2ddfd94fccd86b1e2ad3a4d12bfa65a4dec Mon Sep 17 00:00:00 2001 From: Joscelyn Date: Tue, 11 Jun 2019 16:42:48 -0400 Subject: [PATCH 4/7] WIP: arrays --- README.md | 4 ++-- assignments/array-methods.js | 43 ++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5901c59a5..ad1061662 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ With some basic JavaScript principles in hand, we can now expand our skills out This task focuses on getting practice with higher order functions and callback functions by giving you an array of values and instructions on what to do with that array. -* [ ] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions. +* [x] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions. -* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. +* [x] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. ## Task 3: Array Methods diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f692c35c6..2c17d3ba9 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -56,23 +56,62 @@ 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(function(person) { + fullName.push(person.first_name + ' ' + person.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 = []; + +function nameToUpperCase(person) { + person.first_name = person.first_name.toUpperCase(); +} + +allCaps = runners.map(nameToUpperCase); + 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 = []; +// 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 + + + + +const largeShirts = runners.filter(person => person.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 = []; + +runners.forEach(function(person) { + ticketPriceTotal.push(person.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. From 6d503a3271c1d546e21ffe1464cd6ca24d5f7f7b Mon Sep 17 00:00:00 2001 From: Joscelyn Date: Tue, 11 Jun 2019 16:59:52 -0400 Subject: [PATCH 5/7] finished array file --- assignments/array-methods.js | 40 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/assignments/array-methods.js b/assignments/array-methods.js index 2c17d3ba9..93d6ffa87 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -64,13 +64,13 @@ 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 = []; + function nameToUpperCase(person) { - person.first_name = person.first_name.toUpperCase(); + return person.first_name.toUpperCase(); } -allCaps = runners.map(nameToUpperCase); +const allCaps = runners.map(nameToUpperCase); console.log(allCaps); @@ -98,14 +98,14 @@ 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 = []; - +const ticketPriceTotal = []; +const reducer = (accumulator, currentValue) => accumulator + currentValue; runners.forEach(function(person) { ticketPriceTotal.push(person.donation); }) -console.log(ticketPriceTotal); +console.log(ticketPriceTotal.reduce(reducer)); @@ -116,7 +116,33 @@ console.log(ticketPriceTotal); // 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 +// make a list of donors who contributed at least $100, so they can be given a gift + +const bigDonors = runners.filter(person => person.donation >= 100); + +console.log(bigDonors); + // Problem 2 +// make a list of everyone's email address, so they can be added to a mailing group + +const emails = []; + +runners.forEach(function(person) { + emails.push(person.email); +}) + +console.log(emails); + +// Problem 3 +// all of the runners got hired by lambda. change all of the company names to "Lambda" + + +function hiredByLambda(person) { + person.company = 'Lambda'; + return person; +} + +const lambdaEmployees = runners.map(hiredByLambda); -// Problem 3 \ No newline at end of file +console.log(lambdaEmployees); From 3bd88d06e3afa10b525bd669beeb11ffc90ba2a9 Mon Sep 17 00:00:00 2001 From: Joscelyn Date: Tue, 11 Jun 2019 17:00:47 -0400 Subject: [PATCH 6/7] checked off README --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ad1061662..5bc59eb3d 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ This task focuses on getting practice with higher order functions and callback f Use `.forEach()`, `.map()`, `.filter()`, and `.reduce()` to loop over an array with 50 objects in it. The [array-methods.js](assignments/array-methods.js) file contains several challenges built around a fundraising 5K fun run event. -* [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file. +* [x] Review the contents of the [array-methods.js](assignments/array-methods.js) file. -* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. +* [x] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. -* [ ] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it. +* [x] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it. ## Task 4: Closures @@ -28,10 +28,10 @@ We have learned that closures allow us to access values in scope that have alrea **Hint: Utilize debugger statements in your code in combination with your developer tools to easily identify closure values.** -* [ ] Review the contents of the [closure.js](assignments/closure.js) file. -* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. +* [x] Review the contents of the [closure.js](assignments/closure.js) file. +* [x] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first. ## Stretch Goals -* [ ] Go back through the stretch problems that you skipped over and complete as many as you can. +* [x] 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 From 1aba1d341440c3d257c0353ed1389f675107f65b Mon Sep 17 00:00:00 2001 From: Joscelyn Date: Wed, 12 Jun 2019 10:38:14 -0400 Subject: [PATCH 7/7] finished stretch questions --- .../JavaScript-I/assignments/arrays.js | 370 ++++++++++++++++++ assignments/callbacks.js | 2 +- assignments/closure.js | 32 +- 3 files changed, 398 insertions(+), 6 deletions(-) create mode 100644 assignments/JavaScript-I/assignments/arrays.js diff --git a/assignments/JavaScript-I/assignments/arrays.js b/assignments/JavaScript-I/assignments/arrays.js new file mode 100644 index 000000000..8382dd475 --- /dev/null +++ b/assignments/JavaScript-I/assignments/arrays.js @@ -0,0 +1,370 @@ +$_$wp(1); +let inventory = ($_$w(1, 0), [ + { + 'id': 1, + 'car_make': 'Lincoln', + 'car_model': 'Navigator', + 'car_year': 2009 + }, + { + 'id': 2, + 'car_make': 'Mazda', + 'car_model': 'Miata MX-5', + 'car_year': 2001 + }, + { + 'id': 3, + 'car_make': 'Land Rover', + 'car_model': 'Defender Ice Edition', + 'car_year': 2010 + }, + { + 'id': 4, + 'car_make': 'Honda', + 'car_model': 'Accord', + 'car_year': 1983 + }, + { + 'id': 5, + 'car_make': 'Mitsubishi', + 'car_model': 'Galant', + 'car_year': 1990 + }, + { + 'id': 6, + 'car_make': 'Honda', + 'car_model': 'Accord', + 'car_year': 1995 + }, + { + 'id': 7, + 'car_make': 'Smart', + 'car_model': 'Fortwo', + 'car_year': 2009 + }, + { + 'id': 8, + 'car_make': 'Audi', + 'car_model': '4000CS Quattro', + 'car_year': 1987 + }, + { + 'id': 9, + 'car_make': 'Ford', + 'car_model': 'Windstar', + 'car_year': 1996 + }, + { + 'id': 10, + 'car_make': 'Mercedes-Benz', + 'car_model': 'E-Class', + 'car_year': 2000 + }, + { + 'id': 11, + 'car_make': 'Infiniti', + 'car_model': 'G35', + 'car_year': 2004 + }, + { + 'id': 12, + 'car_make': 'Lotus', + 'car_model': 'Esprit', + 'car_year': 2004 + }, + { + 'id': 13, + 'car_make': 'Chevrolet', + 'car_model': 'Cavalier', + 'car_year': 1997 + }, + { + 'id': 14, + 'car_make': 'Dodge', + 'car_model': 'Ram Van 1500', + 'car_year': 1999 + }, + { + 'id': 15, + 'car_make': 'Dodge', + 'car_model': 'Intrepid', + 'car_year': 2000 + }, + { + 'id': 16, + 'car_make': 'Mitsubishi', + 'car_model': 'Montero Sport', + 'car_year': 2001 + }, + { + 'id': 17, + 'car_make': 'Buick', + 'car_model': 'Skylark', + 'car_year': 1987 + }, + { + 'id': 18, + 'car_make': 'Geo', + 'car_model': 'Prizm', + 'car_year': 1995 + }, + { + 'id': 19, + 'car_make': 'Oldsmobile', + 'car_model': 'Bravada', + 'car_year': 1994 + }, + { + 'id': 20, + 'car_make': 'Mazda', + 'car_model': 'Familia', + 'car_year': 1985 + }, + { + 'id': 21, + 'car_make': 'Chevrolet', + 'car_model': 'Express 1500', + 'car_year': 2003 + }, + { + 'id': 22, + 'car_make': 'Jeep', + 'car_model': 'Wrangler', + 'car_year': 1997 + }, + { + 'id': 23, + 'car_make': 'Eagle', + 'car_model': 'Talon', + 'car_year': 1992 + }, + { + 'id': 24, + 'car_make': 'Toyota', + 'car_model': 'MR2', + 'car_year': 2003 + }, + { + 'id': 25, + 'car_make': 'BMW', + 'car_model': '525', + 'car_year': 2005 + }, + { + 'id': 26, + 'car_make': 'Cadillac', + 'car_model': 'Escalade', + 'car_year': 2005 + }, + { + 'id': 27, + 'car_make': 'Infiniti', + 'car_model': 'Q', + 'car_year': 2000 + }, + { + 'id': 28, + 'car_make': 'Suzuki', + 'car_model': 'Aerio', + 'car_year': 2005 + }, + { + 'id': 29, + 'car_make': 'Mercury', + 'car_model': 'Topaz', + 'car_year': 1993 + }, + { + 'id': 30, + 'car_make': 'BMW', + 'car_model': '6 Series', + 'car_year': 2010 + }, + { + 'id': 31, + 'car_make': 'Pontiac', + 'car_model': 'GTO', + 'car_year': 1964 + }, + { + 'id': 32, + 'car_make': 'Dodge', + 'car_model': 'Ram Van 3500', + 'car_year': 1999 + }, + { + 'id': 33, + 'car_make': 'Jeep', + 'car_model': 'Wrangler', + 'car_year': 2011 + }, + { + 'id': 34, + 'car_make': 'Ford', + 'car_model': 'Escort', + 'car_year': 1991 + }, + { + 'id': 35, + 'car_make': 'Chrysler', + 'car_model': '300M', + 'car_year': 2000 + }, + { + 'id': 36, + 'car_make': 'Volvo', + 'car_model': 'XC70', + 'car_year': 2003 + }, + { + 'id': 37, + 'car_make': 'Oldsmobile', + 'car_model': 'LSS', + 'car_year': 1997 + }, + { + 'id': 38, + 'car_make': 'Toyota', + 'car_model': 'Camry', + 'car_year': 1992 + }, + { + 'id': 39, + 'car_make': 'Ford', + 'car_model': 'Econoline E250', + 'car_year': 1998 + }, + { + 'id': 40, + 'car_make': 'Lotus', + 'car_model': 'Evora', + 'car_year': 2012 + }, + { + 'id': 41, + 'car_make': 'Ford', + 'car_model': 'Mustang', + 'car_year': 1965 + }, + { + 'id': 42, + 'car_make': 'GMC', + 'car_model': 'Yukon', + 'car_year': 1996 + }, + { + 'id': 43, + 'car_make': 'Mercedes-Benz', + 'car_model': 'R-Class', + 'car_year': 2009 + }, + { + 'id': 44, + 'car_make': 'Audi', + 'car_model': 'Q7', + 'car_year': 2012 + }, + { + 'id': 45, + 'car_make': 'Audi', + 'car_model': 'TT', + 'car_year': 2008 + }, + { + 'id': 46, + 'car_make': 'Oldsmobile', + 'car_model': 'Ciera', + 'car_year': 1995 + }, + { + 'id': 47, + 'car_make': 'Volkswagen', + 'car_model': 'Jetta', + 'car_year': 2007 + }, + { + 'id': 48, + 'car_make': 'Dodge', + 'car_model': 'Magnum', + 'car_year': 2008 + }, + { + 'id': 49, + 'car_make': 'Chrysler', + 'car_model': 'Sebring', + 'car_year': 1996 + }, + { + 'id': 50, + 'car_make': 'Lincoln', + 'car_model': 'Town Car', + 'car_year': 1999 + } +]); +$_$w(1, 1), `Car 33 is a *car year goes here* *car make goes here* *car model goes here*`; +function returnCarInfor(id) { + $_$wf(1); + const index = ($_$w(1, 2), id - 1); + const year = ($_$w(1, 3), inventory[index]['car_year']); + const make = ($_$w(1, 4), inventory[index]['car_make']); + const model = ($_$w(1, 5), inventory[index]['car_model']); + return $_$w(1, 6), `Car ${ id } is a ${ year } ${ make } ${ model }.`; +} +$_$w(1, 7), $_$tracer.log(returnCarInfor(33), 'should equal \'Car 33 is a 2011 Jeep Wrangler\'', 'returnCarInfor(33)', 1, 7); +function returnCarInfor(id) { + $_$wf(1); + let index = ($_$w(1, 8), 0); + for (let i = 0; $_$w(1, 9), i < inventory.length; i++) { + if ($_$w(1, 10), inventory[i]['id'] === id) { + $_$w(1, 11), index = i; + } + } + return $_$w(1, 12), `Car ${ id } is a ${ inventory[index]['car_year'] } ${ inventory[index]['car_make'] } ${ inventory[index]['car_model'] }.`; +} +$_$w(1, 13), $_$tracer.log(returnCarInfor(33), 'should equal \'Car 33 is a 2011 Jeep Wrangler\'', 'returnCarInfor(33)', 1, 13); +let lastCar = ($_$w(1, 14), inventory[inventory.length - 1]); +$_$w(1, 15), $_$tracer.log(`${ lastCar['car_make'] } ${ lastCar['car_model'] }`, '`${ lastCar[\'car_make\'] } ${ lastCar[\'ca...', 1, 15); +$_$w(1, 16), $_$tracer.log(`${ lastCar['car_make'] } ${ lastCar['car_model'] }`, 'should equal \'Lincoln Town Car\'', '`${ lastCar[\'car_make\'] } ${ lastCar[\'ca...', 1, 16); +function alphabetize(inventory) { + $_$wf(1); + let carModels = ($_$w(1, 17), []); + for (let element of ($_$w(1, 18), inventory)) { + $_$w(1, 19), carModels.push(element['car_model']); + } + return $_$w(1, 20), carModels.sort(); +} +$_$w(1, 21), $_$tracer.log(alphabetize(inventory), 'alphabetize(inventory)', 1, 21); +function logCarYears(inventory) { + $_$wf(1); + let carYears = ($_$w(1, 22), []); + for (let element of ($_$w(1, 23), inventory)) { + $_$w(1, 24), carYears.push(element['car_year']); + } + return $_$w(1, 25), carYears; +} +$_$w(1, 26), logCarYears(inventory); +$_$w(1, 27), $_$tracer.log(logCarYears(inventory), 'logCarYears(inventory)', 1, 27); +function logOldCars(inventory) { + $_$wf(1); + const carYears = ($_$w(1, 28), logCarYears(inventory)); + let oldCars = ($_$w(1, 29), []); + for (let element of ($_$w(1, 30), carYears)) { + if ($_$w(1, 31), element < 2000) { + $_$w(1, 32), oldCars.push(element); + } + } + return $_$w(1, 33), oldCars.length; +} +$_$w(1, 34), $_$tracer.log(logOldCars(inventory), 'logOldCars(inventory)', 1, 34); +function logBMWAndAudi(inventory) { + $_$wf(1); + let BMWAndAudi = ($_$w(1, 35), []); + for (let i = 0; $_$w(1, 36), i < inventory.length; i++) { + if ($_$w(1, 37), ($_$w(1, 38), inventory[i]['car_make'] === 'Audi') || ($_$w(1, 39), inventory[i]['car_make'] === 'BMW')) { + $_$w(1, 40), BMWAndAudi.push(inventory[i]); + } + } + $_$w(1, 41), $_$tracer.log(BMWAndAudi, 'BMWAndAudi', 1, 41); +} +$_$w(1, 42), $_$tracer.log(JSON.stringify(logBMWAndAudi(inventory)), 'JSON.stringify(logBMWAndAudi(inventory))', 1, 42); +$_$wpe(1); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index 83c81af62..dff76ef34 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -81,7 +81,7 @@ contains('bird', pets, function (boolean) { /* STRETCH PROBLEM */ function removeDuplicates(array, cb) { /* got help from https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array */ - cb(uniqueArray = array.filter(function (item, pos, self) { + cb(uniqueArray = array.filter(function(item, pos, self) { return self.indexOf(item) === pos; })) } diff --git a/assignments/closure.js b/assignments/closure.js index a87a7aa77..bb8f29a6e 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -3,10 +3,12 @@ function minusMaker(minusNumber) { - let result; + + function minus(firstNumber) { - return result = firstNumber - minusNumber; + return firstNumber - minusNumber; + } return minus; @@ -37,11 +39,31 @@ function counter() { return add1; }; const newCounter = counter(); + + console.log(newCounter()); // 1 console.log(newCounter()); // 2 + + + // ==== 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. +const counterFactory = (howMuch) => { + var startNumber = 0; + return ({ + increment: function() { + return startNumber+= howMuch; + }, + + decrement: function() { + return startNumber-= howMuch; + } + }) + }; + +const adderSubtracter = counterFactory( 7); +const add7 = adderSubtracter.increment; +console.log(add7()); + +