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
28 changes: 27 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,54 @@ const runners = [
// ==== 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 and populate a new array called `fullNames`. This array will contain just strings.
let fullNames = [];
runners.forEach(function(runner) {
fullNames.push(`${runner.first_name} ${runner.last_name}`);
})
console.log(fullNames);

// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings.
let firstNamesAllCaps = [];
firstNamesAllCaps = runners.map(runner => runner.first_name.toUpperCase());
console.log(firstNamesAllCaps);

// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects.
let runnersLargeSizeShirt = [];
runnersLargeSizeShirt = runners.filter(runner => runner.shirt_size === 'L');
console.log(runnersLargeSizeShirt);

// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable.
let ticketPriceTotal = 0;
ticketPriceTotal = runners.reduce((acc, runner) => {
return acc += runner.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 a filtered version of the runners array containing only those runners from Skinix
let runnersSkinix = [];
runnersSkinix = runners.filter(runner => runner.company_name === 'Skinix');
console.log(runnersSkinix);

// Problem 2
// Combine the first name, last name, and email addresses of the runners from Skinix into a new array
let runnersFullNamesSkinix = [];
runnersSkinix.forEach(function(runner) {
runnersFullNamesSkinix.push(`${runner.first_name} ${runner.last_name} ${runner.email}`);
})
console.log(runnersFullNamesSkinix);

// Problem 3
// Problem 3
// Add up all of the donations from Skinix runners who donated more than $100
let SkinixSubTotal = 0;
SkinixSubTotal = runnersSkinix
.filter(runner => runner.donation > 100)
.reduce((acc, runner) => {
return acc += runner.donation;
}, 0);
console.log(SkinixSubTotal);
64 changes: 64 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,89 @@ 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);
}
// TEST 1
const test1 = getLength(items, length => `I have ${length} items!`);
console.log(test1); // 'I have 4 items!'

// TEST 2
function purchasedNew(count) {
return `I bought all ${count} items yesterday!`;
}
const test2 = getLength(items, purchasedNew);
console.log(test2); // 'I bought all 4 items yesterday!'


function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length - 1]);
}
// TEST 3
const test3 = last(items, favorite => `I love ${favorite}!`);
console.log(test3); // 'I love Gum!'

// TEST 4
function purchasedToday(element) {
return `I bought ${element} today!`;
}
const test4 = last(items, purchasedToday);
console.log(test4); // 'I bought Gum today!'


function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x + y);
}
// TEST 5
const test5 = sumNums(1, 4, total => `We ate ${total} apples.`);
console.log(test5); // 'We ate 5 apples.'

// TEST 6
function classSchedule(credits) {
return `I am taking ${credits} courses this semester.`
}
const test6 = sumNums(3, 1, classSchedule);
console.log(test6); // 'I am taking 4 courses this semester.'


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

// TEST 7
const test7 = multiplyNums(5, 2, product => `We have ${product} pairs of clean socks left.`);
console.log(test7); // 'We have 10 pairs of clean socks left.'

// TEST 8
function milesToGo(distance) {
return `I have to run ${distance} miles tomorrow.`;
}
const test8 = multiplyNums(2, 3, milesToGo);
console.log(test8); // 'I have to run 6 miles tomorrow.'


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) {
return cb(true);
}
}
return cb(false);
}

// TEST 9a
const test9a = contains('Gum', items, result => `${result}`);
console.log(test9a); // 'true'

// TEST 9b
const test9b = contains('gum', items, result => `${result}`);
console.log(test9b); // 'false'


/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
8 changes: 7 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
// Keep it simple! Remember a closure is just a function
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

const appleCount = 2;
const pearCount = 1;
function returnTotalFruit () {
TotalFruit = appleCount + pearCount;
console.log('I ate ' + TotalFruit + ' pieces of fruit today.');
}
returnTotalFruit();

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

Expand Down