Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ 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 team lead as collaborator on Github.
* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
* [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [ ] Push commits: git push origin `<firstName-lastName>`.
* [X] Create a forked copy of this project.
* [X] Add your team lead 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 `<firstName-lastName>`.
* [X] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [X] Push commits: git push origin `<firstName-lastName>`.

**Follow these steps for completing your project.**

* [ ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
* [ ] Add your team lead as a reviewer on the pull-request
* [X] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
* [X] Add your team lead as a reviewer on the pull-request
* [ ] Your team lead will count the project as complete by merging the branch back into master.

## Task 1: Higher Order Functions and Callbacks

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 2: Array Methods

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 3: Closures

We have learned that closures allow us to access values in scope that have already been invoked (lexical scope).

**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.
* [ ] Look up what an IIFE is in JavaScript and experiment with them
* [X] Go back through the stretch problems that you skipped over and complete as many as you can.
* [X] Look up what an IIFE is in JavaScript and experiment with them
21 changes: 16 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,40 @@ 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 = [];
let fullNames = []
runners.forEach(runner => fullNames.push(`${runner.first_name} ${runner.last_name}`));

console.log(fullNames);

// ==== 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 = [];
let firstNamesAllCaps = runners.map(runner => runner.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 = [];
let runnersLargeSizeShirt = runners.filter(runner => { if (runner.shirt_size === "L") return runner});
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 ticketPriceTotal = runners.reduce((sum, runner) => sum + runner.donation, 0);
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
// Find the average donation.
let averageDonation = runners.reduce((sum, runner) => Math.round(sum + (runner.donation) / runners.length), 0);
console.log(averageDonation);

// Problem 2
// Find all the people who needed large shirts and donated 100 dollars or more so they can get replacement shirts for free.
let freeShirtReplacements = runners.filter(runner => { if (runner.shirt_size === "L" && runner.donation >= 100) return runner});
console.log(freeShirtReplacements);

// Problem 3
// Problem 3
// Create an alphabetised list of company names to make mailers to encourage more employees at each company to particpate next year.
let spamTheseCompanies = runners.map(runner => runner.company_name).sort();
console.log(spamTheseCompanies);
20 changes: 20 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,52 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/

// const testArr = [1, 2, "apples", 3, 4, 5, "pear", 6, 7, 8, 8, "apples"]
// const newVar = []
// function testcb(v) {
// newVar.push(v);
// }

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.
let sum = x + y;
return cb(sum);
}

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
let product = x * y;
return cb(product);
}

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.some(listItem => listItem === item));
}

// contains("apple", testArr, testcb);
// console.log(newVar);

/* 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.
let newArray = [... new Set(array)];
return cb(newArray);
}
// removeDuplicates(testArr, testcb);
// console.log(newVar);
// console.log(testArr);
56 changes: 43 additions & 13 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,60 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

function thisIsTheOuterScope() {
let aVariable = "foo";
function thisIsTheInnerScope() {
return aVariable + " bar";
}
console.log(thisIsTheInnerScope());
}
thisIsTheOuterScope();

/* 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.
};
// 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
const counterMaker = () => {
let count = 0;
const counter = () => count += 1;
return counter;
};
const myCounter = counterMaker();

// ==== 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.
const counterMakerLimit = (limit) => {
let count = 0;
const counter = () => (count < limit ? count += 1 : count = 1);
return counter;
};
const myNewCounter = counterMakerLimit(3);

// ==== 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.
// // ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
// 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 counterFactory = (value) => {
let counter = 0;
const methodObject = {
increment: () => counter += 1,
decrement: () => counter -= 1,
}
if (value === "plus") {
return methodObject.increment;
} else if (value === "minus") {
return methodObject.decrement;
}
};
const myEvenNewerCounter = counterFactory("plus");
const myNewestCounter = counterFactory("minus");