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
88 changes: 78 additions & 10 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// 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.
/*jshint esversion: 6 */

// 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.

Expand Down Expand Up @@ -54,30 +58,94 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","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 = [];

const forEach = (list, callBack) => {
for(let i=0; i<list.length; i++){
callBack(list[i]);
}
};

forEach(runners,function(x){
fullName.push(x.first_name + " " + x.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
// 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);

const x = runners.map((i) => {
return {"fist_name": i.first_name.toUpperCase()};
});
allCaps.push(x);
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
// 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 = [];
const y = runners.filter((i) => {
return i.shirt_size == "L";
});
largeShirts.push(y);
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
// 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 = [];
const z = runners.reduce((i, add) =>
{ return i += add.donation;}, 0);
ticketPriceTotal.push(z);
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.
// 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: The local community center wants to be able to attract more people for next year's
// run, and it is vital to get as many participants as possible. They would like to keep a record of all of the
// email addresses of all of last year's participants

let findEmail = [];

const forEach2 = (list, callBack) => {
for(let i=0; i<list.length; i++){
callBack(list[i]);
}
};

forEach2(runners,function(x){
findEmail.push(x.email);
});

console.log(findEmail);

// Problem 2: the local community center also wants a diverse mix of companies that invest in next year's
// fun run, and that means we do not want all donations coming from the same company, nor any companies
// sounding similar to one another. To do this, they are asking to get the first letter of all of the
// companies that donated this year.

// Problem 1
let companyNamesStartingWithG = [];
const g = runners.map((i) => {
return {"company_name": i.company_name.charAt(0)};
});
companyNamesStartingWithG.push(g);
console.log(companyNamesStartingWithG);

// Problem 2
// Problem 3: the local community center also wants to make sure all contributions are
// larger than $100, so they want to find out the current businesses that donated $100 or more

// Problem 3
let largeDonations = [];
const findLG = runners.filter((i) => {
return i.donation >= 100;
});
largeDonations.push(findLG);
console.log(largeDonations);
69 changes: 62 additions & 7 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// 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.
/*jshint esversion: 6 */

// 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,35 +21,86 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
return cb(arr[0]);
}

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

*/
// */


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

// solution:
function getLength(arr, cb){
return cb(arr.length);
}

// Function invocation
getLength(items, function(second) {
console.log(second);
});


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

//solution:
function last(arr,cb){
return cb(arr[arr.length-1]);
}

//function invocation
last(items,function(third){
console.log(third);
});


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

//solution for HOF:
function HOFNums(x, y, cb) {
return cb(x,y);
}

//solution for sumNums:
function sumNums(x,y){
return x+y;
}

//function invocation:
console.log(HOFNums(5,7,sumNums));

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

//solution for multiplyNums:
function multiplyNums(x,y){
return x*y;
}

//function invocation:
console.log(HOFNums(8,3,multiplyNums));

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.
}

//solution:
function contains(item, list) {
return list.includes(item);
}

//function invocation:
console.log(HOFNums("kiwi",["xy", "dasfdsa", "kiwi"],contains));

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
18 changes: 17 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/*jshint esversion: 6 */

// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function a() {
const a1= "Steak Sauce";
console.log("I sure love steak covered with " + a1);
function b() {
const b4 = "Average Joe's";
console.log("I'm not sure if I have ever gone into that gym " + b4);
function c() {
const c4 = "me cry";
console.log("You're telling me you have never had " + a1 + "? " + b4 + " has no fouler attrocities to speak of! What's next? Are you going to make " + c4 + "?");
}
c();
}
b();
}
a();

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

Expand Down