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
47 changes: 44 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,69 @@ 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(function(items) {
return fullNames.push(`${items.first_name} ${items.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(function(items) {
return items.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(function(items){
return items.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 sum = runners.reduce(function(accumulator, currentValue){
return ticketPriceTotal = 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.

// Problem 1
// You want to know how many attendees are from x company
let attendees = [];

runners.filter(function(items){
return attendees.push(`${items.company_name}`);
});

let attendeesSorted = attendees.sort();

console.log(attendeesSorted);

// Problem 2
// Need to put each runner into alphabetical order by last name
let runnerNames = [];

for (let i=0; i < runners.length; i++) {
runnerNames.push(runners[i].last_name);
}

let runnerNamesSorted = runnerNames.sort();

console.log(runnerNamesSorted);

// Problem 3
// You need to find the email address for each runner
let email = [];

runners.filter(function(items){
return email.push(`${items.email}`);
});

// Problem 3
console.log(email);
36 changes: 34 additions & 2 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,59 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/


function getLength(arr, cb) {
return cb(arr.length);
// getLength passes the length of the array into the callback.
}

let len2 = function(len) {
return len;
}

console.log(getLength(items, len2));

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

console.log(last(items, function(item) {
return items[item.length];
}))


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

const add = (x, y) => x + y;

console.log(add(2,3));

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

const multiply = (x, y) => x * y;

console.log(multiply(2,3));

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.
const note = list.includes(item)
// return cb(list.includes(item));
if(note) {
return cb (true);
} else {
return cb (false);
}
}
console.log (contains("Notebook", items, function(item) {
return item;
}));

/* STRETCH PROBLEM */

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

function genres(genresName) {
const genre = 'Fantasy';
const genre2 = 'Horror';
console.log(`My favorite ${genresName} to write include ${genre} and ${genre2}.`);

function books(booksName) {
const book = 'Lord of the Rings';
const book2 = 'The Walking Dead';
console.log(`${booksName} that inspire me to write include ${book} and ${book2}.`);

function chapters(chaptersName) {
const chaptertotal = 62;
const chaptertotal2 = 193;
console.log(`The ${chaptersName} count for Lord of the Rings is ${chaptertotal} and the chapter/issue count for The Walking Dead is ${chaptertotal2}.`);
}
chapters('chapter');
}
books('Books');
}
genres('genres');
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */


Expand Down