Skip to content
38 changes: 37 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,64 @@ 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(item => {
fullNames.push(`${item.first_name} ${item.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 = [];

firstNamesAllCaps = runners.map(item => item.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 = [];

runnersLargeSizeShirt = runners.filter(item => {
if(item.shirt_size === "L")
return item;
})

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(function(sum, runners) {
return sum + runners.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
// Send confirmation emails to all the runners using their first name in a form email asking that they confirm their spot in the race.
let emailAddresses = [];

emailAddresses = runners.map(item => `${item.first_name} :;: ${item.email}`);

console.log(emailAddresses);

// Problem 2
// Match donation amounts with the company names to verify with the company representative.
let compDonateAmt = [];

compDonateAmt = runners.map(item => `${item.donation} ${item.company_name}`);

console.log(compDonateAmt);

// Problem 3
// Capitalize all last names and match with their ID numbers to print on the shirts as their runner number.
let lastNamesID = [];

lastNamesID = runners.map(item => `${item.id} ${item.last_name.toUpperCase()}`);

// Problem 3
console.log(lastNamesID);
29 changes: 28 additions & 1 deletion assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
return cb(arr[0]);
}

// NOTES ON THE SOLUTION:
// NOTES ON THE SOLUTION:

// firstItem is a higher order function.
// It expects a callback (referred to as `cb`) as its second argument.
Expand All @@ -41,26 +41,53 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr.length);
}
getLength(items, item => console.log(`My length is ${item} items long`));



function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length - 1]);
}
last(items, item => console.log(`My last item is ${item}`));



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

sumNums(1, 2, (x, y) => console.log(`My sum is ${x + y}`));

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

multiplyNums(2, 4, (x, y) => console.log(`My product is ${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.

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

contains('Gum', items, contains => console.log(contains));
contains('Chalk', items, contains => console.log(contains));


/* STRETCH PROBLEM */
fruits = ['Cherries', 'Blueberries', 'Cherries', 'Bananas', 'Oranges', 'Carrots', 'Bananas']
newFruits = []

function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
Expand Down
25 changes: 25 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
// Keep it simple! Remember a closure is just a function
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.
const nums = [2, 4, 6, 8, 10];

function add(num1, num2) {

const added = num1 + num2;
if (num1 in nums){
console.log(`I see a ${num1}`)
console.log(added);
} else {
console.log(`I don't see a ${num1}`)
}

}

add(2, 3);

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

Expand Down Expand Up @@ -31,3 +45,14 @@ const counterFactory = () => {
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
};
const counter = () => {
let count = 0;
return function() {
return ++count;
}
};

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