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
50 changes: 44 additions & 6 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 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.

// 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 @@ -54,30 +54,68 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];

// ==== 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.
// 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((item) => {fullName.push({"Full Name": `${item.first_name} ${item.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 = [];
console.log(allCaps);

allCaps = runners.map((item) => {return {"first_item": item.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 = [];

largeShirts = runners.filter((item) => {
return item.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 = [];

ticketPriceTotal = runners.reduce((total, item) => {
return total = total + item.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
// Problem 1 - // The event director wants the email address of each runner to send a thank you letter to each company representative for their particpation and donation to the community center.

let emails = [];

runners.forEach((item) => {emails.push(item.email)});

console.log(emails);

// Problem 2 - The event director wants a new runners list that has phone key with a value of NULL so that he can add phone numbers later on.

let new_runners = [];

new_runners = runners.map((item) => {item.phone = null; return item});

console.log(new_runners);

// Problem 3 - The event director wants a list of runners that own an email address that ends in .com.

let com_emails = [];

// Problem 2
com_emails = runners.filter((item) => {return item.email[item.email.length-3] === 'c' && item.email[item.email.length-1] === 'm'});

// Problem 3
console.log(com_emails);
60 changes: 60 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,79 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

let portal = "Earth";

function change_location() {
portal = "Mars";

function change_again() {
console.log(portal);
portal = "Jupiter";

function change_last() {
console.log(portal);
portal ="Saturn";
return portal;
}
return change_last();
}
return change_again();
}

console.log(portal); // Earth
change_location(); // Mars and Jupiter
console.log(portal); // Saturn

// ==== Challenge 2: Create a counter function ====

const counter = () => {

// Return a function that when invoked increments and returns a counter variable.

let count = 0;

return () => {
count = count + 1;
return count;
};
};

// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

const newCounter = counter();

console.log(newCounter()); // 1
console.log(newCounter()); // 2
console.log(newCounter()); // 3

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

let add_or_sub = {
"increment": () => {count = count + 1; return count},
"decrement": () => {count = count - 1; return count}
};

return add_or_sub;
};

const inc_dec = counterFactory();

console.log(inc_dec.increment()); // 1
console.log(inc_dec.increment()); // 2
console.log(inc_dec.increment()); // 3
console.log(inc_dec.increment()); // 4

console.log(inc_dec.decrement()); // 3
console.log(inc_dec.decrement()); // 2
console.log(inc_dec.decrement()); // 1
console.log(inc_dec.decrement()); // 0
29 changes: 23 additions & 6 deletions assignments/function-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,30 @@
// };
// add(1,2);

let subtract = function (param1, param2) {
return param1 - param2;
};
subtract(1,2); //?
// let subtract = function (param1, param2) {
// return param1 - param2;
// };
// subtract(1,2);

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

let myFunction = () => {};

let anotherFunction = (param) => param;

let add = (param, param2) => {return param + param2}; // Using the return statement in arrow syntax

add(1,2);

let substract = (param1, param2) => param1 - param2;

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

console.log(triple);