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
39 changes: 30 additions & 9 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small
// donation on their behalf for some much needed updates to their facilities.
// Each business has assigned a representative to attend the event along with a small donation.

// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.

const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"cseiler0@wired.com","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
const runners =
[{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"cseiler0@wired.com","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
{"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"wierland1@angelfire.com","shirt_size":"2XL","company_name":"Wordtune","donation":148},
{"id":3,"first_name":"Julieta","last_name":"McCloid","email":"jmccloid2@yahoo.com","shirt_size":"S","company_name":"Riffpedia","donation":171},
{"id":4,"first_name":"Martynne","last_name":"Paye","email":"mpaye3@sciencedaily.com","shirt_size":"XL","company_name":"Wordware","donation":288},
Expand Down Expand Up @@ -54,30 +57,48 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}];

// ==== 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.
// 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((runner)=> fullName.push(`${runner.first_name} ${runner.last_name}`));
Copy link
Collaborator

Choose a reason for hiding this comment

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

With Arrow Function Syntax, if you only have ONE parameter, you don't need to wrap them in parenthesis! they are optional! (two or more params would require parenthesis though!)

runners.forEach( 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 = [];
console.log(allCaps);
let allCaps = runners.map((runner) => 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 = [];
let largeShirts = runners.filter((runner)=> runner.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 = [];
let ticketPriceTotal = runners.reduce((acc, currentValue) => {
return acc = acc + 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
// Organize the data into array that shows the company alongside the donation amount dorted by highest donation.
let topGivers = runners.map((runner) => {
return {"company": runner.company_name, "donation": runner.donation};

})
let sortedTopGivers = topGivers.sort((a,b) => b.donation - a.donation)
console.log(sortedTopGivers);
// Problem 2

// Problem 3
// Make an easy to read contact list of Just the First and Last name plus Email
let contactList = runners.map((runner)=> {
return `${runner.first_name} ${runner.last_name} - E-mail: ${runner.email}`
});
console.log(contactList);
// Problem 3
// List all the runners and sort alphabetically
let nameList = runners.map((runner)=> {
return `${runner.first_name} ${runner.last_name}`
});
let sortedNameList = nameList.sort();
console.log(sortedNameList);
44 changes: 30 additions & 14 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
// Create a higher order function and invoke the callback function to test your work.
// You have been provided an example of a problem and a solution to see how this works with our items array.
// Study both the problem and the solution to figure out the rest of the problems.


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.
}
Expand All @@ -17,39 +19,53 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
return cb(arr[0]);
}

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

*/

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

function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
}
// getLength passes the length of the array into the callback.
cb("Array Length is " + arr.length);
};
getLength(items, console.log);



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

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

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

// 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) {
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
cb(list.includes(item))
}
contains('Gum', items, console.log);

/* STRETCH PROBLEM */

// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
let newArray = array.filter( item => item);
cb(newArray)
}
8 changes: 7 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function greetWorld(greeting){
let hello = greeting;
(function(){
console.log(`${hello} World!`);
})();
}
greetWorld("greetings");

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

Expand Down