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
62 changes: 58 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,82 @@ 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(runner) {
fullName.push(runner.first_name + ' ' + runner.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 = [];
runners.map(function(runner) {
allCaps.push(runner.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 = [];
runners.filter(function(runner) {
if (runner.shirt_size === 'L') {
largeShirts.push(runner);
}
//return largeShirts.push(runner.shirt_size === 'L'); <-- Didn't work this way
});
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 (total, runner) { //(accumulator, currentValue)
return total + runner.donation;
}, 0);
console.log(ticketPriceTotal);

// let tickerPriceTotal = [];
// runners.forEach(function(runner) {
// ticketPriceTotal.push(runner.donation);
// });
// console.log(ticketPriceTotal.reduce(function (acc, val, ind, arr) {
// return acc + val;
// }));



// ==== 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
// Problem 1 - The companies participating will receive certificates of appreciation. Make an array of all the companies participating and alphabetize the array.

let companyList = []
runners.forEach(function(runner) {
companyList.push(runner.company_name);
});
companyList.sort();
console.log(companyList);

// Problem 2 - Special recognition will go to student and government-employed runners. Find all the runners that have .edu or .gov email addresses. Display the number of runners and the array.

let emailList = [];
runners.filter(function(runner) {
if (runner.email.endsWith("edu") || runner.email.endsWith("gov")) {
emailList.push(runner);
}
})
console.log(emailList.length, emailList);


// Problem 3 The local community center wants to host the event again but they don't want to invite the smallest donation contributors. Find the runners who donated less than 50 so they do not receive an invitation for the next event. Also find the total of the smallest donations.

// Problem 2
let smallDonators = [];
runners.filter(function(runner) {
if (runner.donation < 50) {
smallDonators.push(runner);
}
})
let smallDonationTotal = smallDonators.reduce(function (total, runner) {
return total + runner.donation;
}, 0);

// Problem 3
console.log(smallDonationTotal);
console.log(smallDonators);
25 changes: 25 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,52 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

*/

/* Callback Function */
function giveResult(parameter) {
let result = parameter;
return 'The result is ' + result;
}
/* CB Function ends */

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

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

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

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

function giveBoolResult(boolParameter) {
return boolParameter;
}

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 (list[i] === item) {
return true;
}
}
return false; //RETURN false needs to be outside the for loop and if statement, otherwise whenever the first index value is not equal to item being checked it will return false right away and not check the other items
}
console.log(contains('yo-yo', items, giveBoolResult));

/* STRETCH PROBLEM */

Expand Down
16 changes: 16 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

let name = 'Edgar';
let age = 28;
if (true === true) {
let lastName = 'Flores';
console.log(name);
console.log(lastName);
name = 'Who';
}
console.log(name);


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

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

Expand Down