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
53 changes: 47 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,69 @@ 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(function(person)
{
return fullName.push(person.first_name + " " + person.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 = runners.map(function(person)
{
return person.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 = runners.filter(function(data) {
if (data.shirt_size === "L") {
return data;
}
});

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 = runners.reduce(function (money, moreMoney) {
return money += moreMoney.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: Use .forEach to return a list of email addresses for the 5k email list.

let emailList = [];

runners.forEach(function (person) {
return emailList.push(person.email);
});

console.log(emailList);

// Problem 2: Use .map to change all the donations to double their amounts.

let moreMoney = runners.map(function (person) {
return person.donation * 2;
});

console.log(moreMoney);

// Problem 3: Use .forEach to get a list of companies participating in the event.

let companies = [];

runners.forEach(function (person) {
return companies.push(person.company_name);
});

// Problem 2
console.log(companies);

// Problem 3
64 changes: 58 additions & 6 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,82 @@
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

// firstItem passes the first item of the given array to the callback function.

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
return cb(arr);
}

function first(items) {
return(items[0]);
}

firstItem(items, first);

// getLength passes the length of the array into the callback.

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

function length(items) {
return(items.length);
}

getLength(items, length);

// last passes the last item of the array into the callback.

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

function final(items) {
return (items[items.length - 1]);
}

last(items, final);

// sumNums adds two numbers (x, y) and passes the result to the callback.

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

function add(x, y) {
return x + y;
}

sumNums(3, 4, add);

// multiplyNums multiplies two numbers and passes the result to the callback.

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) {
function multiply(x, y) {
return x * y;
}

multiplyNums(3, 4, multiply);

// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.

function contains(item, list, cb) {
return cb(item, list);
}

function includes(item, list) {
if (list.includes(item)) {
return true;
} else {
return false;
}
}

contains('Pencil', items, includes); //returns true

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
32 changes: 31 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function whatFruit() {
let person = "I";

function eatTheFruit() {
let action = "eat";
console.log(person + " like to eat.");

function fruit() {
console.log(person + " like to " + action + " bananas!");
}

fruit();
}

eatTheFruit();
}

whatFruit();

// ==== Challenge 2: Create a counter function ====
// Return a function that when invoked increments and returns a counter variable.

const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0;
return function () {
count += 1;
return count
}
};
const newCounter = counter();
console.log(newCounter());
console.log(newCounter());
newCounter();
newCounter();//returns 4

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