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
49 changes: 39 additions & 10 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,57 @@ 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 = [];
console.log(fullNames);
runners.forEach(function(object) {
let name = object.first_name + " " + object.last_name;
fullNames.push(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 = [];
console.log(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 = [];
console.log(runnersLargeSizeShirt);
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 and save the total into a ticketPriceTotal variable.
let ticketPriceTotal = 0;
console.log(ticketPriceTotal);
let ticketPriceTotal = runners.reduce(function(sum, donations) {
return sum + donations.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
// Problem 1: The event director needs the company name of each runner for their running bibs. Populate a new array called `companyNames`. This array will contain just strings.

let companyNames = [];
runners.forEach(function(object) {
let name = object.company_name + " ";
companyNames.push(name);
})
// console.log(companyNames);


// Problem 2: The small 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 small sized shirts so they can choose a different size. This will be an array of objects.

// Problem 2
let smallShirts = [];
smallShirts = runners.filter((runner) => {
return runner.shirt_size === "S";
});
// console.log(smallShirts);

// Problem 3
// Problem 3: The event director needs to find premium runners who donated more than $200. Populate a new array called `biggestDonor`. We need a filtered version of the runners array, containing only the runners with largest amount of donation. This will be an array of objects. Use .filter()
let premiumDonors = [];
premiumDonor = runners.filter((donations) => {
return donations.donation > 200;
});
// console.log(premiumDonor);
112 changes: 80 additions & 32 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,117 @@
// 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'];

//* 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'];
//* firstItem passes the first item of the given array to the callback function.
/*

// GIVEN THIS PROBLEM:
//* GIVEN THIS PROBLEM:

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
//* firstItem passes the first item of the given array to the callback function.
}

// SOLUTION:
//* SOLUTION:

function firstItem(arr, cb) {
return cb(arr[0]);
}

// NOTES ON THE SOLUTION:
//* NOTES ON THE SOLUTION:

// firstItem is a higher order function.
// It expects a callback (referred to as `cb`) as its second argument.
// To test our solution, we can use the given `items` array and a variety of callbacks.
// Note how callbacks can be declared separately, or inlined.
//* firstItem is a higher order function.
//* It expects a callback (referred to as `cb`) as its second argument.
//* To test our solution, we can use the given `items` array and a variety of callbacks.
//* Note how callbacks can be declared separately, or inlined.

// TEST 1 (inlined callback):
//* TEST 1 (inlined callback):

const test1 = firstItem(items, item => `I love my ${item}!`);
console.log(test1); // "I love my Pencil!"
console.log(test1); //* "I love my Pencil!"

// TEST 2 (declaring callback before hand):
//* TEST 2 (declaring callback before hand):

function logExorbitantPrice(article) {
return `this ${article} is worth a million dollars!`;
};

const test2 = firstItem(items, logExorbitantPrice);
console.log(test2); // "this Pencil is worth a million dollars!"
console.log(test2); //* "this Pencil is worth a million dollars!"
*/

// Done below

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
//* firstItem passes the first item of the given array to the callback function.

function firstItem(arr, cb) {
return cb(arr[0]);
};
//* TEST 1 (inlined callback):
const test1 = firstItem(items, item => `I love my ${item}!`);
//* console.log(test1);

function logExorbitantPrice(article) {
return `this ${article} is worth a million dollars!`;
};
//* TEST 2 (declaring callback before hand):
const test2 = firstItem(items, logExorbitantPrice);
//* console.log(test2);

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

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

function last(arr, cb) {
// last passes the last item of the array into the callback.
//* getLength passes the length of the array into the callback.
return cb(arr.length);
}
const test4 = getLength(items, arrLength => `This list is ${arrLength} items long!`);
//* console.log(test4);

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
//* sumNums adds two numbers (x, y) and passes the result to the callback.
//? Higher Order Function & arrow function
return cb(x, y);
}
let sum = (x, y) => x + y;
console.log(sumNums(3, 4, sum));


function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
//* multiplyNums multiplies two numbers and passes the result to the callback.
//? Higher Order Function
return cb(x, y);
}
let multiply = (function(x, y) {
return (x * y)
});
// console.log(multiplyNums(3, 4, 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.
return cb(item, list);}
//* contains checks if an item is present inside of the given array/list.
//* Pass true to the callback if it is, otherwise pass false.
let conditional = function(item, list) { return list.includes(item)
}
// console.log(contains('Pencil', items, conditional))




/* STRETCH PROBLEM */
const duplicateItems = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Pencil', 'Notebook', 'yo-yo', 'Gum']

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);
}
let duplicates = removeDuplicates(duplicateItems, function(arr) {
return [...new Set(arr)];
})
console.log(duplicates);

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.
}
116 changes: 83 additions & 33 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,83 @@
// ==== Challenge 1: Write your own closure ====
// Write a closure of your own creation.
// 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.


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


// ==== Challenge 2: Implement a "counter maker" function ====
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`!
// 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!
// 3- Return the `counter` function.
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2

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

// ==== 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.
};
//* ==== Challenge 1: Write your own closure ====
//* Write a closure of your own creation.
//* 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.

let me = "Catherine Kim"
function greetMe() {
console.log("Hello, " + me + "!")
}
me = "Batman"
// greetMe()

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

//* ==== Challenge 2: Implement a "counter maker" function ====
//* 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`!
//* 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!
// //* 3- Return the `counter` function.
//* };
//* Example usage: const myCounter = counterMaker();
//* myCounter(); //* 1
//* myCounter(); //* 2

// const counterMaker = () => {
// let count = 0;
// return function () {
// count++
// return count;
// }
// };
// const newCounter = counterMaker();
// console.log(newCounter());
// console.log(newCounter());
// console.log(newCounter());
// console.log(newCounter());

//* ==== 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.
//? Did IFFE here as the last Stretch.

const counterMaker = (function() {
let count = 0;
let limit = 10;

while(count < limit) {
console.log(count);
count++;
}
let newCount = count;
return newCount;
}());
console.log(counterMaker);

//* ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
//* 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 counterMaker = () => {
// let lessNum = 10;
// let num = 0;
// decrement: lessNum = lessNum - 1;
// console.log(lessNum);
// console.log(lessNum);
// console.log(lessNum);

// CLOSURE
// return counterFactory = function() {
// increment: num = num + 1;
// return num;
// }
// };

// const newNum = counterMaker();
// console.log(newNum());
// console.log(newNum());
// console.log(newNum());
// console.log(newNum());
Binary file added node_modules/.cache/esm/.data.blob
Binary file not shown.
1 change: 1 addition & 0 deletions node_modules/.cache/esm/.data.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.