Skip to content
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ Use `.forEach()`, `.map()`, `.filter()`, and `.reduce()` to loop over an array w
The [closure.js](assignments/closure.js) assignment showcases how variables can be used outside of functions to store state using closure.

* Complete each challenge.

12:00 PM PDT - Project START!
44 changes: 39 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// A local community center is holding a fund rasising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
// A local community center is holding a fundraising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.

// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.

Expand Down Expand Up @@ -56,28 +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(obj => fullName.push(`${obj.first_name} ${obj.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 = [];
// let allCaps = [];
let allCaps = runners.map(obj => obj.first_name.toUpperCase());
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 = [];
let largeShirts = runners.filter(obj => obj.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 = [];
// let ticketPriceTotal = [];
let ticketPriceTotal = runners.reduce((theReducer, obj) => theReducer + obj.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 solve 3 unique problems using one or many of the array methods listed above.

// Problem 1
/* The most obvious problem is figuring out the average donation. We can simply use reduce to sum all donations, then divide by length of runners*/
let donationTotal = runners.reduce((theReducer, obj) => theReducer + obj.donation, 0); //Exactly the same as challenge 4
let donationAvg = donationTotal / runners.length;
console.log(donationAvg);

// Problem 2
/* Now that we know the average donation, perhaps we'd like to shower a little...extra attention to donors who donated more than the average. We should
identify the companies that are especially generous.

// Problem 3
To state more formally, we will return an array of strings representing companies that donated above average, with each string containing the name of the company and the amount they donated. The strings will be ordered by the amount of donation, from highest to smallest.*/
let generousCompanies = runners.filter(obj => obj.donation > donationAvg)
.sort((a, b) => b.donation - a.donation)
.map(obj => `Company ${obj.company_name} donated ${obj.donation}.`);
console.log(generousCompanies);
/* We sorted the companies by most generous to generous, and tidied up the output to a nice readable string using map */

// Problem 3
/* As for the companies that donated below the average, they must be going through hard times. It's understandable. Their employees might not even be paid enough to eat proper meals. Because of our compassion and understanding, we will issue the shirt sizes for these poor people a size smaller than original, so that their poverty will not be so blatantly obvious.

Formally, we will return a new array containing people of companies that donated below the average and their corresponding *ahem* "corrected" shirt sizes. Like problem 2, we will output each person-shirt pair as a string so our t-shirt supplier will understand what to do. */
const shirtSizes = ["XS", "S", "M", "L", "XL", "2XL", "3XL"];

const sympathySize = obj => {
let sizeIndex = shirtSizes.indexOf(obj.shirt_size);
obj.shirt_size = sizeIndex - 1 > 0 ? shirtSizes[sizeIndex - 1] : "XS";
return obj;
};

let miserablePeople = runners.filter(obj => obj.donation < donationAvg)
.sort((a, b) => shirtSizes.indexOf(a.shirt_size) - shirtSizes.indexOf(b.shirt_size))
.map(sympathySize)
.map(obj => `${obj.first_name} ${obj.last_name}'s Shirt Size is ${obj.shirt_size}`);

console.log(miserablePeople);
40 changes: 39 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

/* Here's one from the precourse days */

function room() {
let personA = 34;
return function() {
let personB = 35;
return personA + personB;
}
}

let whatAreTheyDoing = room();
console.log(whatAreTheyDoing());

// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0;
return function() {
count++;
return count;
}
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

const newCounter = counter();
console.log(newCounter());
console.log(newCounter());

// ==== 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.
// `decrement` should decrement the counter variable and return it.
let count = 0;
return {
increment: function() {
count++;
return count;
},
decrement: function() {
count--;
return count;
}
}
};

let letsCount = counterFactory();
console.log(letsCount);
console.log(letsCount.increment());
console.log(letsCount.increment());
console.log(letsCount.decrement());
console.log(letsCount.decrement());
console.log(letsCount.decrement());
23 changes: 15 additions & 8 deletions assignments/function-conversion.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax

// let myFunction = function () {};

let myFunction = () => {};
// let anotherFunction = function (param) {
// return param;
// };

let anotherFunction = param => param;
console.log(anotherFunction("Hello")); //-> Hello
// let add = function (param1, param2) {
// return param1 + param2;
// };
// add(1,2);

let subtract = function (param1, param2) {
return param1 - param2;
};
subtract(1,2); //?
let add = (param1, param2) => param1 + param2;
console.log(add(1,2)); //-> 3
/*/////////////////////////////////////*/
// let subtract = function (param1, param2) {
// return param1 - param2;
// };
let subtract = (param1, param2) => param1 - param2;
console.log(subtract(1,2)); //-> -1

exampleArray = [1,2,3,4];
// const triple = exampleArray.map(function (num) {
// return num * 3;
// });
// console.log(triple);
// console.log(triple);

const triple = exampleArray.map(num => num * 3);
console.log(triple);