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
38 changes: 35 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,60 @@ 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 = [];
const names = runners.forEach((runner) => {
return 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 = [];
const UpperCaseNames = runners.map((runner) => {
return firstNamesAllCaps.push(`${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 => {
return 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;
const donationTotal = runners.reduce((accumulator, runner) => {
return ticketPriceTotal = accumulator + 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
//return the number of all shirt sizes
let shirtSizeXS = runners.filter(runner => {return runner.shirt_size === "XS"}).length;
let shirtSizeS = runners.filter(runner => {return runner.shirt_size === "S"}).length;
let shirtSizeM = runners.filter(runner => {return runner.shirt_size === "M"}).length;
let shirtSizeL = runners.filter(runner => {return runner.shirt_size === "L"}).length;
let shirtSizeXL = runners.filter(runner => {return runner.shirt_size === "XL"}).length;
let shirtSizeXXL = runners.filter(runner => {return runner.shirt_size === "2XL"}).length;
let shirtSizeXXXL = runners.filter(runner => {return runner.shirt_size === "3XL"}).length;
let shirtCount = `XS:${shirtSizeXS} S:${shirtSizeS} M:${shirtSizeM} L:${shirtSizeL} XL:${shirtSizeXL} 2XL:${shirtSizeXXL} 3XL: ${shirtSizeXXXL}`
console.log(shirtCount);

// Problem 2

// Problem 3
//return people who donated a minimum of 50
let minDonators = runners.filter(runner => {
return runner.donation >= 50
});
console.log(minDonators)
// Problem 3
//return names alphabetically last name first
let lastFirstNames = [];
const convertNames = runners.forEach((runner) => {
return lastFirstNames.push(`${runner.last_name} ${runner.first_name}`);
});
namesSorted = lastFirstNames.sort()
console.log(namesSorted)
24 changes: 24 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,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, length => `This array is ${length} long`);
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 => `The last item is ${item}`)
console.log(test2)

function add(x, y){
return x + y
}
function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x, y)
}
const test3 = sumNums(3, 4, add);
console.log(test3)

function multiply(x, y){
return x * y
}
function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x, y)
}
const test4 = multiplyNums(3, 5, multiply);
console.log(test4)

function trueOrFalse(item, list){
return list.includes(item)
}
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)
}
const test5 = contains("Pencil", items, trueOrFalse)
console.log(test5)

/* STRETCH PROBLEM */

Expand Down
7 changes: 6 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// 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 yogi = "Yogi Bear"
function description(){
const likes = "pic-a-nic baskets";
return (`${yogi} loves ${likes}`);
}
console.log(description());

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

Expand Down