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
55 changes: 46 additions & 9 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for
//some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.

// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.

Expand Down Expand Up @@ -56,30 +57,66 @@ 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.
// 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 (namePull => {
return fullNames.push(`${namePull.first_name} ${namePull.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.
// 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(u =>(u.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.
// 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(size => size.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;
console.log(ticketPriceTotal);
// 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.




const ticketPriceTotal = runners.reduce(function (accumulator, currentValue) {
return accumulator + currentValue.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.
// 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
// Anyone that donated 200 or more will receieve a free hat.
const freeHat = runners.filter(function (donate){
return donate.donation >= 200
})
console.log(freeHat)


// Problem 2
// A investor wants to donate $200 on any donation of $200 or more
const donDouble = freeHat.map(function (double){
return double.donation + 200
})
console.log(donDouble)
// Problem 3

// Problem 3
const donationTotal = donDouble.reduce(function (a, value){
return a + value;
}, 0)
console.log(donationTotal);

21 changes: 17 additions & 4 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// 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'];


console.log(items[items.length -1])
/*

// GIVEN THIS PROBLEM:
Expand All @@ -12,8 +16,8 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

// SOLUTION:

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

// NOTES ON THE SOLUTION:
Expand Down Expand Up @@ -41,19 +45,27 @@ 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)
}
getLength( items, l => console.log(`my length is ${l}`))

function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length -1])
}
last(items, lItem => console.log(`last in the array is ${lItem}`))

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x + y)
}
sumNums(2,6, sum => console.log(`${sum}`))

function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x * y)
}
multiplyNums(3,9, product => console.log(`${product}`))

function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
Expand All @@ -66,4 +78,5 @@ 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.

}
9 changes: 9 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

function outerFunc(){
let searchEngine = "google";

return function(){
console.log (`just ${searchEngine} it`)
}
}
var say = outerFunc();
say();

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

Expand Down