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
24 changes: 20 additions & 4 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,44 @@ 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(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 = [];
let 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 = [];
let 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;
let ticketPriceTotal = runners.reduce(((total, runner) => total += 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
// You need a list of all of the donating companies so that you can send thank you notes after the event.

let companies = runners.map(runner => runner.company_name);
console.log(companies);

// Problem 2
// You need a list of all participants' alphabetized by last name for when they check in for the race, but include their first name, last name, and company.

let checkinList = runners.map(runner => `${runner.last_name}, ${runner.first_name} -- ${runner.company_name}`);
checkinList = checkinList.sort();
console.log(checkinList);

// Problem 3
// You need to have a head count of how many people are participating so you can have enough water cups at each water station during the race.

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

function arrLength(num) {
return num;
}

const itemsLength = getLength(items, arrLength);
console.log('\nChallenge 1:');
console.log(itemsLength);



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

function lastItem(element) {
return element;
}

const myLast = last(items, lastItem);
console.log('\nChallenge 2:');
console.log(myLast);


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

function sum(num) {
return num;
}

const mySum = sumNums(12, 2, sum);
console.log('\nChallenge 3:');
console.log(mySum);


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

function multiply(num) {
return num;
}

const myMultiple = multiplyNums(3, 4, multiply);
console.log('\nChallenge 4:');
console.log(myMultiple);


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.
return cb(list.includes(item));
}

function doesItInclude(bool) {
return bool;
}

const isIncluded = contains('Highlighter', items, doesItInclude);

console.log('\nChallenge 5:');
console.log(isIncluded);

/* STRETCH PROBLEM */

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

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.
return cb(array.filter((item, index) => array.indexOf(item)=== index));
}

function arrNoDuplicates(arr) {
return arr;
}

const myNewArr = removeDuplicates(items2, arrNoDuplicates);
console.log('\nStretch Goal:\n');
console.log('Duplicates removed:\t');
console.log(myNewArr);
console.log('Original array:\t');
console.log(items2);
62 changes: 62 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

const double = (number) => {
let num = 2;

return function() {
num = num * 2;
return number * num;
}
};

const myConst = double(3);
console.log(myConst());
console.log(myConst());
console.log(myConst());

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

Expand All @@ -12,22 +25,71 @@
const counterMaker = () => {
// IMPLEMENTATION OF counterMaker:
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
let count = 0;
// 2- Declare a function `counter`. It should increment and return `count`.
// NOTE: This `counter` function, being nested inside `counterMaker`,
// "closes over" the `count` variable. It can "see" it in the parent scope!
const counter = () => {
const limit = 10;
if (count > limit) {
count = 0;
}
return ++count;
}
// 3- Return the `counter` function.
return counter;
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
const myCounter = counterMaker();
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());
console.log(myCounter());


// ==== Challenge 3: Make `counterMaker` more sophisticated ====
// It should have a `limit` parameter. Any counters we make with `counterMaker`
// will refuse to go over the limit, and start back at 1.

// Work for challenge 3 is with challenge 2

// ==== Challenge 4: 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.

let counter = 0;

return {
increment: function() {
return ++counter;
},
decrement: function() {
return --counter;
},
};
};

let myCount = counterFactory();

console.log('Starting counterFactory...\n');
console.log(myCount.increment());
console.log(myCount.increment());
console.log(myCount.increment());
console.log(myCount.increment());
console.log(myCount.decrement());
console.log(myCount.decrement());