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
30 changes: 29 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,56 @@ 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 = [];
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 = [];
runners.map((runner) => firstNamesAllCaps.push(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 = [];
runners.filter((runner) => {
if (runner.shirt_size === "L") {
runnersLargeSizeShirt.push(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;
ticketPriceTotal = runners.reduce((acc, runner) => acc + 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
// Last 10 runners with sizes larger than small
lateshirtSizes = [];
runners.filter(runner => {
if (runner.id > 40 & !runner.shirt_size.includes("S")) {
lateshirtSizes.push(runner);
}
});
console.log(lateshirtSizes);

// Problem 2
// List out email addresses next to the first name
let nameEmail = [];
runners.forEach(runner => nameEmail.push(`${runner.first_name}, ${runner.email}`));
console.log(nameEmail);

// Problem 3
// Problem 3
// Total donation by students(edu email)
let studentDonation =
runners.filter(runner => {
return runner.email.includes("edu");
}).reduce((acc, runner) => {
return acc + runner.donation;
}, 0);
console.log(studentDonation);
Comment on lines +107 to +113

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice chaining!

18 changes: 18 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,45 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/

function callbackTest(a, b, cb){
return cb(a, b);
}

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return arr.length;
}
console.log(callbackTest(items, "", getLength));

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

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

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return x * y;
}
console.log(callbackTest(4, 5, multiplyNums));

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.
for (let i = 0; i < list.length; i++) {
if (item === list[i]) {
return true;
}
return false;
}
}
console.log(callbackTest('Pencil', items, contains));

/* STRETCH PROBLEM */

Expand Down
8 changes: 8 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

const parent = () => {
let number = 1;
const child = () => {
console.log(`I am ${number} level deep`);
};
child();
};
parent();

/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */

Expand Down