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
40 changes: 38 additions & 2 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,64 @@ 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(element=>{
fullName.push(`${element.first_name} ${element.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 = [];
allCaps = runners.map(x => x.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 = []
largeShirts = runners.filter(elem => elem.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((accumulator, currentValue) => accumulator + currentValue.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
// Create an email list to notify users of event changes.
let emailList = [];
runners.forEach(element=>{
emailList.push(element.email);
});
console.log(emailList);

// Problem 2
// The runners get different goodie bags depending on the size of their donations. The following code creates three arrays of runners for the three different bags types.
// Donation of 0-100
let standardBag = runners.filter(elem => elem.donation <= 100);
// Donation of 100-200
let deluxBag = runners.filter(elem => elem.donation >= 100 && elem.donation <= 200 );
// Donation of 200+
let premiumBag = runners.filter(elem => elem.donation >= 200);

// Problem 3
// Log the arrays
console.log(standardBag);
console.log(deluxBag);
console.log(premiumBag);

// Problem 3
// The people handing out the bags dont need all of the data in the bag arrays. The following code gives them a list of first and last names for each bag type.
let standardBagList= [];
let deluxBagList = [];
let premiumBagList = [];

standardBag.forEach(element=> {standardBagList.push(`${element.first_name} ${element.last_name}`); });
deluxBag.forEach(element=> {deluxBagList.push(`${element.first_name} ${element.last_name}`); });
premiumBag.forEach(element=> {premiumBagList.push(`${element.first_name} ${element.last_name}`); });

console.log(standardBagList);
console.log(deluxBagList);
console.log(premiumBagList);
85 changes: 64 additions & 21 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,92 @@

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

/*

//Given this problem:

// //Given this problem:

function firstItem(arr, cb) {
// 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.
// }

// Potential Solution:
// // Potential Solution:

// Higher order function using "cb" as the call back
function firstItem(arr, cb) {
return cb(arr[0]);
}
// // Higher order function using "cb" as the call back
// function firstItem(arr, cb) {
// return cb(arr[0]);
// }

// Function invocation
firstItem(items, function(first) {
console.log(first)
});
// // Function invocation
// firstItem(items, function(first) {
// console.log(first)
// });

*/


// 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.length);
}
// Function invocation
getLength(items,(length)=> {
console.log(length)
});


// 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[ arr.length -1 ]);
}
// Function invocation
last(items,(lastItem)=> {
console.log(lastItem)
});


// 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 invocation
sumNums(3, 7, function (sum) {
console.log(sum);
});


// multiplies two numbers (x, y) 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 invocation
multiplyNums(3, 7, function (product) {
console.log(product);
});


// checks if an item is present inside of the given array/list. Pass True or False.
// METHOD 1 Using .includes
function contains(item, list, cb) {
cb(list.includes(item));
}
// Function invocation
contains('yo-yo', items, function (contains) {
console.log(contains);
});

//Method 2 Using .forEach
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.
let check= false;
list.forEach(function(element) {
if(element === item){
check= true;
}
})
cb(check);
}
// Function invocation
contains(3, items, function (contains) {
console.log(contains);
});

/* STRETCH PROBLEM */

Expand Down
14 changes: 14 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
// A closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time the closure was created.

function makeAdder(x) {
// variables avalabel X
return function(y) {
// Variables avalable x and y
return x + y;
};
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2)); // 7
console.log(add10(2)); // 12

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

Expand Down