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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
node_modules
102 changes: 100 additions & 2 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,127 @@ 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.
// FIRST ATTEMPT
// let fullNames = [];

// function firstAndLast(names) {
// return (`${names.first_name} ${names.last_name}`);
// }
// runners.forEach(function.push(names));
// console.log(fullNames);

// SECOND TRY WITH MDN DOCS AS REFERENCE
// let fullNames = [];
// runners.forEach(function(names) {
// fullNames.push(names);
// });
// console.log(fullNames);

// THIRD ATTEMPT REFERENCING SLACK CHAT
// let fullNames = [];
// runners.forEach(function(runners) {
// fullNames.push(runners);
// });
// console.log(fullNames);

// FOURTH ATTEMPT REFERENCING FOREACH BREAKOUT SOLUTION
let fullNames = [];
runners.forEach(function(names) {
return fullNames.push(`${names.first_name} ${names.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 = [];
// FIRST ATTEMPT
// var firstNamesAllCaps = runners.map(function(first_name) {
// first_name = first_name.toUpperCase();
// return first_name;
// });
// console.log(firstNamesAllCaps);

let firstNamesAllCaps = runners.map(function(runner) {
return runner.first_name.toUpperCase();
});

console.log(firstNamesAllCaps);
console.log(runners);

// ==== 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 = [];
runners.filter(function(runners){
if(runners.shirt_size === "L") {
return runnersLargeSizeShirt.push(`${runners.first_name} ${runners.last_name}`);
}
});
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;
for(let i= 0; i < runners.length; i++) {
ticketPriceTotal += runners[i].donation;
}
const finalDonation = runners.reduce((total, donations) => {
return total += donations.donation;
}, 0);
console.log(ticketPriceTotal);
console.log(finalDonation);

// ==== 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
// The event organizer needs a list of companies to put on the sponsor banner. Also, try to remove duplicates.
// EXAMPLE
// let uniqueCompanies = [];
// for (let i = 0; i < runners.length; i++) {
// if (uniqueCompanies.indexOf(runners[i].company_name) === -1) {
// uniqueCompanies.push(runners[i].company_name);
// }
// }

let companies = [];
runners.forEach(runners => {
if (companies.indexOf(runners.company_name) === -1) {
companies.push(runners.company_name);
}
});
companies.sort();

console.log(companies);

// Problem 2
// The race organizer needs everyone's email addresses to send them updates about the race. map()
const mailingList = runners.map(
function(email) {
return `${email.first_name} ${email.last_name}, ${email.email}`;
}
);

console.log(mailingList);

// Problem 3
// The race organizer is going to group people into waves based on their last name, so they need an alphabetical list of all race participants ordered by last name.

// const lastNameFirst = runners.sort(
// function(lastName)
// return `${lastName.last_name}`
// )
// console.log(runners.sort());

// let lastNamesFirst = [];
// runners.forEach(names => {
// if (lastNamesFirst).indexOf(names.last_name) === -1) {
// lastNamesFirst.push(names.last_name);
// }
// });
// lastNamesFirst.sort();

let lastNamesFirst = [];
runners.forEach(names => lastNamesFirst.push(`${names.last_name}, ${names.first_name};`));
lastNamesFirst.sort();

// Problem 3
console.log(lastNamesFirst);
23 changes: 21 additions & 2 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,6 +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.

const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Pencil'];

/*

Expand Down Expand Up @@ -41,29 +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);
}
getLength(items, console.log);

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

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


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

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(list.includes(item));
}
contains("Pencil", items, console.log);
contains("Eraser", items, console.log);

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
const newArray = [];
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
array.forEach(index => {
if (newArray.indexOf(index) === -1) {
newArray.push(index);
}
});
return cb(newArray);
}
removeDuplicates(items, console.log);
28 changes: 24 additions & 4 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,39 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

let pirate = (function() {
let firstName = "Jack";
return function() {
let lastName = "Sparrow";
let bestPirate = firstName + " " + lastName;
return bestPirate;
}
})();

pirate();

console.log(pirate());

/* 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 = () => {
const counterMaker = (function () {
let count = 0;
return function counter() {
count += 1;
return count;
};
})();
// 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!
// 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.
};

console.log(counterMaker());
console.log(counterMaker());
console.log(counterMaker());
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand Down
22 changes: 11 additions & 11 deletions assignments/index.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>JS II</title>
<title>JS II</title>

<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
</head>
<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
</head>

<body>
<h1>JS II - Check your work in the console!</h1>
</body>
<body>
<h1>JS II - Check your work in the console!</h1>
</body>
</html>