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: 18 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,40 @@ 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((item) => {
fullNames.push({'fullname': item.first_name + " " + item.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((item) => {
return item.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((item) => {
return item.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, item) => {
return total += item.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 2
// Problem 1 - filter donations above 200
let filterDonations = runners.filter((item) => {
return item.donation > 200;
});
console.log(filterDonations);
// Problem 2 -

// Problem 3
27 changes: 26 additions & 1 deletion assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// 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.
// 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'];

Expand Down Expand Up @@ -41,25 +43,48 @@ 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);
}

const test1 = getLength(items, item => `There are ${item} items in the list!`);
console.log(test1);

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

const test2 = last(items, item => `I love my ${item}!`);
console.log(test2);

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

const test3 = sumNums(3, 9, data => `The sum of these two numbers is ${data}!`);
console.log(test3);

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

const test4 = multiplyNums(3, 9, data => `The product of these two numbers is ${data}!`);
console.log(test4);

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

const test5 = contains('Gum', items, data => `It is "${data}" that this item is present in the given list!`);
console.log(test5);

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
24 changes: 24 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,43 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

const a = 7
function multiplyByTwo(x) {
let ret = x * 2;
return ret;
}

let b = multiplyByTwo(a);
console.log(b)

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


// ==== Challenge 2: Implement a "counter maker" function ====
// let count = 0
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!

function counter() {
count++
return count
}

// 3- Return the `counter` function.
return counter

};

const myCounter = counterMaker();
console.log(myCounter())
console.log(myCounter())
console.log(myCounter())

// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand Down