Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
014d684
callbacks assignment complete (no stretch)
kyranmccann Sep 11, 2018
e2a024f
array methods complete (partial stretch)
kyranmccann Sep 11, 2018
bcbec4a
closure assignment (no stretch)
kyranmccann Sep 11, 2018
33d544a
array methods stretch completed.
kyranmccann Sep 11, 2018
9b47792
minor change to array methods stretch
kyranmccann Sep 11, 2018
82f3c45
extra array methods stretch because I'm having fun
kyranmccann Sep 11, 2018
5e908d9
callbacks stretch
kyranmccann Sep 11, 2018
eb1e841
commenting out my "didn't want to scroll up" array of company names.
kyranmccann Sep 11, 2018
2f39a48
finishing comments
kyranmccann Sep 11, 2018
ea04a5d
closure stretch (i think....)
kyranmccann Sep 12, 2018
3b3b896
tested the closure stretch
kyranmccann Sep 12, 2018
95cd065
updated closure stretch to accept number argument
kyranmccann Sep 12, 2018
08d2f00
more versatile versions of array-methods stretch
kyranmccann Sep 12, 2018
0dbb73d
changing those updates to arrow functions
kyranmccann Sep 12, 2018
60cfd26
fixing a missing semicolon.
kyranmccann Sep 12, 2018
d9691ec
cleaning it up, changing the closure stretch as per convo with PM
kyranmccann Sep 12, 2018
e8d60a6
cleaning up the code on closures
kyranmccann Sep 12, 2018
dc3bcc2
more cleaning
kyranmccann Sep 12, 2018
e2d85b8
cleaning up counter because it shouldn't have worked (but did)
kyranmccann Sep 12, 2018
c0d3336
Working on array-methods extra stretch go get this down.
kyranmccann Sep 12, 2018
59cff49
managed to delete another line?
kyranmccann Sep 12, 2018
f77686a
another few lines gone...
kyranmccann Sep 12, 2018
113ef1d
successfully invoked two callback functions.
kyranmccann Sep 12, 2018
210f418
changing companies to search
kyranmccann Sep 12, 2018
7666dd1
taking out commented old functions because it was too hard to read.
kyranmccann Sep 12, 2018
f35c5ed
order of functions
kyranmccann Sep 12, 2018
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
99 changes: 93 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,117 @@ 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}`);
});
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) =>{
// allCaps.push(runner.first_name.toUpperCase());
// })
let allCaps = runners.map((runner) => {
return 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 = [];

let largeShirts = runners.filter((runner) => {
return 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 = [];
let ticketPriceTotal = runners.reduce((acccumulator, currentValue ) => {
return acccumulator + 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
// Company teams

makeCompanyTeam = (company, arr) => {
let companyTeam = arr.filter((entry) => {
return entry.company_name === company;
});
return companyTeam;
};
console.log(makeCompanyTeam('Skinix', runners));
console.log(makeCompanyTeam('Gigashots', runners));


// Problem 2
//Team fundraising

companyTeamTotal = (company, arr, cb) => {
let companyTotal = cb(company, arr).reduce((accumulator, runner) => {
return accumulator + runner.donation;
}, 0);
return companyTotal;
};
console.log(companyTeamTotal('Skinix', runners, makeCompanyTeam));
console.log(companyTeamTotal('Gigashots', runners, makeCompanyTeam))

//Problem 3
//Company with the highest average donation

let companies = [];
runners.forEach((runner) => {
companies.push(`${runner.company_name}`);
});

let uniqueCompanies = companies.filter(function(item, pos) {
return companies.indexOf(item) == pos;
});


const findHighestAverage = (search, array, teamcb, totalcb) => {
let currentHighestAverage = 0;
let currentHighestCompany = [];
search.forEach((company) => {
let currentTeamAverage = (totalcb(company, runners, teamcb)) / teamcb(company, runners).length;
if (currentTeamAverage > currentHighestAverage){
currentHighestAverage = currentTeamAverage;
currentHighestCompany = company;
}
});
return `${currentHighestCompany}\'s team raised an average of $${currentHighestAverage} per person!`;
};

console.log(findHighestAverage(uniqueCompanies, runners, makeCompanyTeam, companyTeamTotal));

// Problem 4
//Special gift email list for donating over $100.

let specialGift = runners.filter((runner) => {
return runner.donation > 100;
});
let specialGiftEmails = [];
specialGift.forEach((runner) => {
specialGiftEmails.push(`${runner.first_name} ${runner.last_name}. Donation Amount: $${runner.donation}. Email: ${runner.email}`);
});
console.log(specialGiftEmails);

//Problem 5 because this is fun
//Average donation

// Problem 3
let totalDonations = runners.reduce((accumulator, runner ) => {
return accumulator + runner.donation;
}, 0);
let averageDonation = totalDonations/runners.length;
console.log(averageDonation);
42 changes: 39 additions & 3 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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 @@ -24,29 +24,65 @@ 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, function(length){
console.log(length);
})

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

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

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, function(add){
console.log(add);
})

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

multiplyNums(3,9, function(multiply){
console.log(multiply);
})
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('Paper', items, function(isit){
console.log(isit);
});

contains('Gum', items, function(isit){
console.log(isit);
});


/* STRETCH PROBLEM */

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 uniqueArray = array.filter(function(item, pos, self) {
return self.indexOf(item) == pos;
})
cb(uniqueArray);
}

removeDuplicates(items, function(test){
console.log(test);
});
55 changes: 53 additions & 2 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function greeting(namePara) {
const name = namePara;
const welfareCheck = 'How are you doing today';
console.log(`Hey, ${name}!`);


function ask() {
console.log(`${welfareCheck}, ${name}?`);

} // ask

ask();
} //greeting

greeting('Kyran');


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

const newCounter = counter();

console.log(newCounter ());
console.log(newCounter ());
console.log(newCounter ());

// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

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

// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {

// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
const counterFactory = () => {
let count = 0;
return {
increment: () => {
return count += 1;
},
decrement: () => {
return count -= 1;

}
};
}

// Testing:
console.log(counterFactory());


const isItWorking= counterFactory();

console.log(isItWorking.increment());
console.log(isItWorking.decrement());

console.log(isItWorking.decrement());
console.log(isItWorking.increment());
console.log(isItWorking.increment());
console.log(isItWorking.increment());