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
82 changes: 65 additions & 17 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// 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.
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some
//information from the businesses.

const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"[email protected]","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
const runners = [
{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"[email protected]","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
{"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"[email protected]","shirt_size":"2XL","company_name":"Wordtune","donation":148},
{"id":3,"first_name":"Julieta","last_name":"McCloid","email":"[email protected]","shirt_size":"S","company_name":"Riffpedia","donation":171},
{"id":4,"first_name":"Martynne","last_name":"Paye","email":"[email protected]","shirt_size":"XL","company_name":"Wordware","donation":288},
Expand Down Expand Up @@ -51,33 +55,77 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"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":"[email protected]","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 into a new array called fullName.
let fullName = [];
console.log(fullName);
let fullName = runners.forEach(function(currentValue){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fullName = runners.forEach(function(currentValue){
runners.forEach(function(currentValue){

forEach has no return value, unlike map.

// console.log(currentValue.first_name, currentValue.last_name);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// console.log(currentValue.first_name, currentValue.last_name);
fullName.push(`${currentValue.first_name} ${currentValue.last_name}`);

So since forEach doesn't have a return value, we have to manually populate the fullName array

});

// console.log(fullName);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// console.log(fullName);
console.log(fullName);

Right now this is undefined, after you follow my suggestion it should contain the correct answer





// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
let allCaps = [];
console.log(allCaps);
// The event director needs to have all the runner's first names converted to uppercase because
//the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result

const allCaps = runners.map(currentValue => currentValue.first_name.toUpperCase());


// console.log(allCaps);




// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
let largeShirts = [];
console.log(largeShirts);
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with
//large sized shirts so they can choose a different size. Return an array named largeShirts that contains
//information about the runners that have a shirt size of L and log the result

let largeShirts = runners.filter(currentValue => currentValue.shirt_size === "L");


// console.log(largeShirts);





// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result
let ticketPriceTotal = [];
console.log(ticketPriceTotal);
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a
//ticketPriceTotal array and log the result

let ticketPriceTotal = runners.reduce((total, currentValue) => {
return total += 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
//As a token of our appreciation, we want to make sure our top doners receive a special 'Thank-You' card. Sort out all of the doners who donated $150 or more.
const topDoners = runners.filter(currentValue => currentValue.donation > 150);

// console.log(topDoners);



// Problem 2
//Using .forEach and template literals, create a list of the participants first and last name, as well as their email.
let contactInfo = runners.forEach(function(currentValue){
// console.log(`${currentValue.first_name} ${currentValue.last_name}, ${currentValue.email}`);
});


// Problem 3
let fidel = runners.filter(currentValue => currentValue.last_name === "Fidel");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fidel = runners.filter(currentValue => currentValue.last_name === "Fidel");
let fidel = runners.filter(currentValue => currentValue.first_name === "Fidel");

Fidel is someone's first name in the array


// Problem 3
console.log(fidel);
65 changes: 55 additions & 10 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +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.
// 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'];

Expand All @@ -24,28 +26,71 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

*/


function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
const length = function(array){

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So they were looking for these questions to be answered in a little bit of a different format than what you have here, but you correctly demonstrated using HOFs with callbacks

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


function last(arr, cb) {
// last passes the last item of the array into the callback.

// last passes the last item of the array into the callback.
const lastItem = function(array){
return array.pop();
}
function last(array, cb) {
return cb(array);
}
console.log(last(items, lastItem));



// sumNums adds two numbers (x, y) and passes the result to the callback.
const add = function(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);
}
console.log(sumNums(2, 8, add));





// multiplyNums multiplies two numbers and passes the result to the callback.
const multiply = function(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);
}
console.log(multiplyNums(3, 3, multiply));


function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.
//const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
// 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 includesItem = function(item, list){
if (list.includes(item)) {
return true;
} else {
return false;
}
}
function contains(item, list, cb) {
return cb(item, list);
}

console.log(contains("Pencil", items, includesItem));






/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
15 changes: 15 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function godThreat() {
const god = "I am a God Threat monster";
console.log(`Fear me, for ${god}`);

function dragonThreat() {
console.log(`I am a Dragon Threat. Only those who can say ${god} are stronger than me`);
}
dragonThreat();
}
godThreat();






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

Expand Down
2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script src="array-methods.js"></script>
<script src="callbacks.js"></script>
<script src="closure.js"></script>
<script src="stretch-function-conversion.js"></script>
<!-- <script src="stretch-function-conversion.js"></script> -->
</head>

<body>
Expand Down