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
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@ 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
* [ ] Your team lead will count the project as complete by merging the branch back into master.
* [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
* [x] 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.

Expand Down
48 changes: 40 additions & 8 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,62 @@ 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.
// 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 = [];
runners.forEach(function(el){
fullNames.push(`${el.first_name} ${el.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 = [];
// 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(function(el) {
return el.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 = [];
// 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(function(el){
return el = el.shirt_size === 'L';
});
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(function(ac, el){
return ac += el.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.
// 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
// The event director wants to recognize donors that donated more than 250. Populate a new array, highRollers, for donors over 250
const highRollers = runners.filter(function(el) {
return el = el.donation >= 250
});
console.log(`High Rollers (donors over 250):`, highRollers);

// Problem 2
// The event director is interested in knowing what the avarage donation. Using the runners array find the average of donations.
const avgDonation = runners.reduce(function(total, amount){
return total += amount.donation
}, 0) / runners.length;

// Problem 3
console.log(`Average Donation: ${avgDonation}`)

// Problem 3
// The event director is planning ahead! The event director is looking at starting an email newsletter. Create a list of all email addresss so the director can start spamming for next year
let emailList = []
runners.forEach(function(el){
emailList.push(el.email);
})
console.log(emailList);
56 changes: 40 additions & 16 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// 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'];

Expand All @@ -12,9 +14,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

// SOLUTION:

function firstItem(arr, cb) {
return cb(arr[0]);
}


// NOTES ON THE SOLUTION:

Expand All @@ -23,47 +23,71 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
// To test our solution, we can use the given `items` array and a variety of callbacks.
// Note how callbacks can be declared separately, or inlined.

// TEST 1 (inlined callback):
*/

const test1 = firstItem(items, item => `I love my ${item}!`);
console.log(test1); // "I love my Pencil!"
// function firstItem(arr, cb) {
// return cb(arr[0]);
// }

// TEST 2 (declaring callback before hand):
// // TEST 1 (inlined callback):

function logExorbitantPrice(article) {
return `this ${article} is worth a million dollars!`;
};
// const test1 = firstItem(items, item => `I love my ${item}!`);
// console.log(test1); // "I love my Pencil!"

// // TEST 2 (declaring callback before hand):

// function logExorbitantPrice(article) {
// return `this ${article} is worth a million dollars!`;
// };

// const test2 = firstItem(items, logExorbitantPrice);
// console.log(test2); // "this Pencil is worth a million dollars!"

const test2 = firstItem(items, logExorbitantPrice);
console.log(test2); // "this Pencil is worth a million dollars!"
*/

function logger(log) {
return log;
}

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(`The length of the array is: ${arr.length}`);
}

console.log(getLength(items, logger))

function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length - 1]);
}

console.log(last(items, logger));

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x + y);
}

console.log(sumNums(3, 5, logger));

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.
let iExist = list.includes(item);

return cb(`Tis be ${iExist}`);
}

console.log(contains('Pencil', items, logger))

/* 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.
}
// Do not mutate the original array.
}
61 changes: 60 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

function shazam() {
var firstName = 'Mr. Jonah';

function sayMyName() {
var lastName = 'Aitchison'
console.log(`Hello, ${firstName} ${lastName}`);
}

sayMyName();
}
shazam();

/* 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 = () => {
const counterMaker = (limit) => {
let count = 0;

function counter() {
if (count >= limit){
return count = 1
}else{
return ++count;
}
}

return counter;
// 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`.
Expand All @@ -20,14 +42,51 @@ const counterMaker = () => {
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
const myCounter = counterMaker(5);
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());

// ==== 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.

// ^^ Look above for Challenge 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.

let counter = 0;

let increment = function(num){
counter += num;
console.log(counter)
}

let decrement = function(num){
counter -= num;
console.log(counter)
}

return {increment: increment, decrement: decrement}

};

const myCounterFactory = counterFactory();

console.log(myCounterFactory.increment(1))
console.log(myCounterFactory.increment(1))
console.log(myCounterFactory.increment(1))
console.log(myCounterFactory.increment(1))
console.log(myCounterFactory.decrement(2))