Skip to content
Merged
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
124 changes: 121 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,146 @@ 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(item => {
var eachName = `${item.first_name}, ${item.last_name}`;
fullName.push(eachName);
return fullName;
});
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( eachRunner => {
allCaps.push(eachRunner['first_name'].toUpperCase());
return allCaps;

})
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 filteredRunners = runners.filter(largeRunners => {
return largeRunners.shirt_size === 'L';
});
largeShirts.push(filteredRunners);
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 total = runners.reduce((totalMoney, currentRunnerDonation) => {
return totalMoney + currentRunnerDonation.donation;
}, 0);
ticketPriceTotal.push(total);
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
// Problem 1 : Total donations from each size shirt,
// will have to use filter to filter out each shirt size. Will have to use reduce to get the total amount.

// Avg. donation from 'xs' runners
let filteredXSRunners = runners.filter(xsRunners => {
return xsRunners.shirt_size === 'XS';
})
let XSRunnerTotalDonation = filteredXSRunners.reduce((allXSRunnerMoney, currentXSRunnerMoney) => {
return allXSRunnerMoney + currentXSRunnerMoney.donation;
}, 0);
// console.log(`${XSRunnerTotalDonation}`);
let XSRunnerAvgDonation = XSRunnerTotalDonation / filteredXSRunners.length;
console.log(`Average Donation from Runners with xs size: ${XSRunnerAvgDonation}`);

// Avg. donation from 's' runners
let filteredSRunners = runners.filter(sRunners => {
return sRunners.shirt_size === 'S';
})
let SRunnerTotalDonation = filteredSRunners.reduce((allSRunnerMoney, currentSRunnerMoney) => {
return allSRunnerMoney + currentSRunnerMoney.donation;
}, 0);
let SRunnerAvgDonation = SRunnerTotalDonation / filteredSRunners.length;
console.log(`Average Donation from Runners with s size: ${SRunnerAvgDonation}`);

// Avg. donation from 'M' runners
let filteredMRunners = runners.filter(MRunners => {
return MRunners.shirt_size === 'M';
})
let MRunnerTotalDonation = filteredMRunners.reduce((allMRunnerMoney, currentMRunnerMoney) => {
return allMRunnerMoney + currentMRunnerMoney.donation;
}, 0);
let MRunnerAvgDonation = MRunnerTotalDonation / filteredMRunners.length;
console.log(`Average Donation from Runners with M size: ${MRunnerAvgDonation}`);

// Avg. donation from 'L' runners
let filteredLRunners = runners.filter(LRunners => {
return LRunners.shirt_size === 'L';
})
let LRunnerTotalDonation = filteredLRunners.reduce((allLRunnerMoney, currentLRunnerMoney) => {
return allLRunnerMoney + currentLRunnerMoney.donation;
}, 0);
let LRunnerAvgDonation = LRunnerTotalDonation / filteredLRunners.length;
console.log(`Average Donation from Runners with L size: ${LRunnerAvgDonation}`);

// Avg. donation from 'XL' runners
let filteredXLRunners = runners.filter(XLRunners => {
return XLRunners.shirt_size === 'XL';
})
let XLRunnerTotalDonation = filteredXLRunners.reduce((allXLRunnerMoney, currentXLRunnerMoney) => {
return allXLRunnerMoney + currentXLRunnerMoney.donation;
}, 0);
let XLRunnerAvgDonation = XLRunnerTotalDonation / filteredXLRunners.length;
console.log(`Average Donation from Runners with XL size: ${XLRunnerAvgDonation}`);

// Avg. donation from 'twoXL' runners
let filteredtwoXLRunners = runners.filter(twoXLRunners => {
return twoXLRunners.shirt_size === '2XL';
})
let twoXLRunnerTotalDonation = filteredtwoXLRunners.reduce((alltwoXLRunnerMoney, currenttwoXLRunnerMoney) => {
return alltwoXLRunnerMoney + currenttwoXLRunnerMoney.donation;
}, 0);
let twoXLRunnerAvgDonation = twoXLRunnerTotalDonation / filteredtwoXLRunners.length;
console.log(`Average Donation from Runners with 2XL size: ${twoXLRunnerAvgDonation}`);

// Avg. donation from 'threeXL' runners
let filteredthreeXLRunners = runners.filter(threeXLRunners => {
return threeXLRunners.shirt_size === '3XL';
})
let threeXLRunnerTotalDonation = filteredthreeXLRunners.reduce((allthreeXLRunnerMoney, currentthreeXLRunnerMoney) => {
return allthreeXLRunnerMoney + currentthreeXLRunnerMoney.donation;
}, 0);
let threeXLRunnerAvgDonation = threeXLRunnerTotalDonation / filteredthreeXLRunners.length;
console.log(`Average Donation from Runners with 3XL size: ${threeXLRunnerAvgDonation}`);

// Conclusion: Medium sized individuals are the stingiest, 3XL sized individuals are the most generous!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great conclusion. Great use of methods as well.


// let shirtSizeDonationObj = {
// '3XL': `${threeXLRunnerAvgDonation}`,
// '2XL': `${twoXLRunnerAvgDonation}`
// }
// console.log(shirtSizeDonationObj);

// Problem 2 : Companies with donation of more than $200.
// need to use filter and map;
let highestDonatingCompanies = runners.filter(highestDonatingCompany => {
return highestDonatingCompany.donation > 200;
});
let highestDonatingCompanyNames = highestDonatingCompanies.map(companyName => {
return companyName.company_name;
});
console.log(highestDonatingCompanyNames);


// Problem 3 : find highest donation

// Problem 2
let eachDonor = runners.map(eachPerson => {
return eachPerson['donation'];
});
console.log(eachDonor);
let highestDonation = eachDonor.reduce((previousLargeNumber, currentLargeNumber) => {
return (currentLargeNumber > previousLargeNumber) ? currentLargeNumber : previousLargeNumber;
}, 0);
console.log(highestDonation);

// Problem 3
37 changes: 37 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,66 @@ 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, length => console.log(length));


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great use of arrow functions here.

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


function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
cb(x, y);
}
const addFunc = (a, b) => console.log(a + b);
sumNums(1, 7, addFunc);


function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
cb (x, y);
}
const multiplyFunc = (a, b) => console.log(a * b);
multiplyNums(2, 4, multiplyFunc);


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) {
cb();
} else {
return false;
}
}
}
const answerForContains = str => console.log(true);
// contains('Pencil', items, answerForContains);
contains('Smart Phone', items, answerForContains);

/* STRETCH PROBLEM */

const arrayOfDuplicates = ['Onion', 'Garlic', 'Bell Pepper', 'Garlic', 'Ginger', 'Onion', 'Garlic'];


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 newArr = [];
for (let i = 0; i < array.length; i++) {
if (!newArr.includes(array[i])) {
newArr.push(array[i]);
}
}
cb(newArr);
}
const joiningFunc = anArr => console.log(anArr.join('--'));
removeDuplicates(arrayOfDuplicates, joiningFunc)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was a really great solution to the stretch.

38 changes: 38 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
const kingdom = (continentName) => {
console.log(`Welcome to ${continentName}`);
const region = () => {
let continentRegion = 'The North';
console.log(`${continentRegion} in ${continentName} will freeze you over`);
function castle() {
let continentCastle = 'Winterfell'
console.log(`Winter is coming to ${continentCastle} in ${continentRegion} of ${continentName}`);
}
castle();
}
region();
}
kingdom('Westeros');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wonderful use of Game of Thrones.


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



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

Expand All @@ -17,4 +41,18 @@ 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.
let count = 0;
let counterObj = {
incrementor: function() {
return count++;
},
decrementor: function() {
return count--;
}
}
counterObj.incrementor();
counterObj.decrementor();

};
// console.log(counterFactory());

13 changes: 11 additions & 2 deletions assignments/stretch-function-conversion.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax

// let myFunction = function () {};
let myFunction = () => {};

// let anotherFunction = function (param) {
// return param;
// };
let anotherFunction = (param) => param;
console.log(anotherFunction('Hangry'));

// let add = function (param1, param2) {
// return param1 + param2;
// };
// add(1,2);
let add = (param1, param2) => param1 + param2;
console.log(add(7, 1));


// let subtract = function (param1, param2) {
// return param1 - param2;
// };
// subtract(1,2);
let subtract = (param1, param2) => param1 - param2;
console.log(subtract(17, 9));

// exampleArray = [1,2,3,4];
exampleArray = [1,2,3,4];
// const triple = exampleArray.map(function (num) {
// return num * 3;
// });
// console.log(triple);
const triple = exampleArray.map((num) => num * 3);
console.log(triple);