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
35 changes: 29 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,57 @@ const runners = [
{ id: 47, first_name: "Vida", last_name: "Tydd", email: "[email protected]", shirt_size: "S", company_name: "Quaxo", donation: 55 },
{ id: 48, first_name: "Anderea", last_name: "MacGiolla Pheadair", email: "[email protected]", shirt_size: "2XL", company_name: "Kwimbee", donation: 214 },
{ id: 49, first_name: "Bel", last_name: "Alway", email: "[email protected]", shirt_size: "S", company_name: "Voolia", donation: 107 },
{ id: 50, first_name: "Shell", last_name: "Baine", email: "[email protected]", shirt_size: "M", company_name: "Gabtype", donation: 171 },
{ id: 50, first_name: "Shell", last_name: "Baine", email: ".edu", shirt_size: "M", company_name: "Gabtype", donation: 171 },
];

// ==== 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(element){

return fullNames.push(`${element.first_name} ${element.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(runners =>runners.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(runners =>{ return runners.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, runner) =>{ return total += 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
// Problem 1 Student discount - filter all runners with a .edu email
let emaillist = runners.filter( runners => runners.email.includes(".edu"));

// runners.forEach(function(name){
// // if (name.email.(".edu"))
// return emaillist.push(`${name.email}`)
// });



console.log(emaillist)
// Problem 2
let LastNamesAllCaps = runners.map(runners =>{ return runners.last_name.toUpperCase()});

console.log(LastNamesAllCaps);
// Problem 3

let highID = runners.filter( runner => {return runner.id > 25});

// Problem 3
console.log(highID);
72 changes: 53 additions & 19 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
// GIVEN THIS PROBLEM:

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.

}

// SOLUTION:
Expand Down Expand Up @@ -38,32 +38,66 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/

// getLength passes the length of the array into the callback.

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

function last(arr, cb) {
getLength(items, function(length) {
return console.log(length);
});

// last passes the last item of the array into the callback.
}

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
}
function last(arr, cb) {
return cb(arr[arr.length - 1])
};

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

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.
}
// sumNums adds two numbers (x, y) and passes the result to the callback.

function sumNums(num1, num2 ,cb) {
return cb(num1 + num2);
}

sumNums(3, 10, function(summer){
return console.log(summer);
});

// multiplyNums multiplies two numbers and passes the result to the callback.

/* STRETCH PROBLEM */
function mutliplyNums(x, y, cb){
return cb(x * y);
}

mutliplyNums(10,11, function(multi){
return console.log(multi)
});

// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.

function contains(arr, searchitem, cb){
if (arr.includes(searchitem)) {
return cb(true);
}
else {
return cb(false);
}
}

contains(items, 'Gum', function(search){
return console.log(search)
});



/* STRETCH PROBLEM */

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.
}

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

var me = 'Spencer'
function greetMe(){
console.log(`Hello, my name is ${me}`)
}
greetMe()

/* 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 = () => {
// 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!
// 3- Return the `counter` function.
};
// let count = 0;
// const counterMaker = () => {

// function counter(){
// return count;
// };
// };
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand Down
3 changes: 2 additions & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</head>

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