Skip to content

finished callbacks #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
99 changes: 96 additions & 3 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,121 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
// ==== 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 = [];
//i did the the old way for testing purposes
// for(let i = 0; i < runners.length; i++){
// fullName.push(`${runners[i].first_name} ${runners[i].last_name}`);
// }
// console.log(fullName);

runners.forEach(function(names){
Copy link
Collaborator

Choose a reason for hiding this comment

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

perfect way to do this. you could also refactor this to ES6 fat arrow syntax.

runners.forEach(names => 
  fullName.push(`${names.first_name} ${names.last_name}`)
);


fullName.push(`${names.first_name} ${names.last_name}`)
//console.log(value.first_name)
});
console.log(fullName);

// ==== 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 = [];
allCaps = runners.map(function(names) {
return names.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 = [];

largeShirts = runners.filter(shirt => shirt.shirt_size === 'L')
Copy link
Collaborator

Choose a reason for hiding this comment

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

excellent use of the fat arrow.


console.log(largeShirts);

// ==== Challenge 4: Use .reduce() ====
// // ==== 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 = [];

ticketPriceTotal = runners.reduce(function(ticket,item){
Copy link
Collaborator

Choose a reason for hiding this comment

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

this could also be refactored into ES6

ticketPriceTotal = runners.reduce((ticket,item) => 
  ticket += item.donation, 0);


return ticket += item.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: Find which company donated the most and output: companyName donationValue.
let maxVal = 0;
let biggestCompany = [];

runners.forEach(function(value){
if(value.donation > maxVal){
maxVal = value.donation;
}
});

runners.filter(function(value){
if(value.donation === maxVal){
console.log(`${value.company_name} ${maxVal}`);
return;
}
});


// Problem 2: if donations are over 50 dollars then gather the emails of the individuals to send a special thank you.
let runnerMail = [];
let runnerabv50 = [];
runnerabv50 = runners.filter(function(value){
return value.donation > 50;
});


runnerabv50.forEach(function(value){

runnerMail.push(`${value.email}`)

});
console.log(runnerMail)

// Find out who wears a size small in the list of runners.
let sizeSmall = [];

runners.filter((runner) => {

if(runner.shirt_size === "S"){

sizeSmall.push(runner.first_name + " " + runner.last_name);
}
});
console.log(sizeSmall);

// Problem 2
//The t-shirt supplier needs to know how many of each kind of shirt they will need to ship. Make an array for each size.
let sCount = [];
sCount = runners.filter((runner) =>{
return runner.shirt_size === 'S';
})
let mCount = [];
mCount = runners.filter((runner) =>{
return runner.shirt_size === 'M';
})
let lCount = [];
lCount = runners.filter((runner) =>{
return runner.shirt_size === 'L';
})
let xlCount = [];
xlCount = runners.filter((runner) =>{
return runner.shirt_size === 'XL';
})
let xxlCount = [];
xxlCount = runners.filter((runner) =>{
return runner.shirt_size === '2XL';
})
let xxxlCount = [];
xxxlCount = runners.filter((runner) =>{
return runner.shirt_size === '3XL';
})

console.log(`We will need ${sCount.length} small, ${mCount.length} medium, ${lCount.length} large, ${xlCount.length} xl, ${xxlCount.length} xxl, and ${xxxlCount.length} 3xl shirts.`);

// Problem 3
71 changes: 59 additions & 12 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,101 @@

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


// Potential Solution:

// Higher order function using "cb" as the call back
function firstItem(arr, cb) {
return cb(arr[0]);
}

// Function invocation
firstItem(items, function(first) {
console.log(first)
});

*/


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


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

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

veryLast(items, function(last) {
console.log(last);
});


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

function sumNums(x, y, cb){
return cb(x + y);
}

sumNums(5, 7, function(addition){
console.log(addition)
});


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

function multiplyNums(x, y, cb) {
return cb(x * y);
}

multiplyNums(2, 5, function(multiplication){
console.log(multiplication)
});


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

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

contains('yo-yo', items, function(trueFalse){
console.log(trueFalse)
});
/* 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.
let newArray = array.map(item => {return item});
}


24 changes: 23 additions & 1 deletion assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!

function sayHello(name) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

good job with this. you could also strip it down a bit more, the template literal does not require parens, and you can drop the curly braces from the say function.

function sayHello(name) {
  const text = `Hello ${name}`;
  const say = () => console.log(text);
  say();
}

const text = (`Hello ${name}`);
const say = function() { console.log(text); }
say();
}
sayHello('Andy');

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


// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0;
return(function() { return( ++count); })
Copy link
Collaborator

Choose a reason for hiding this comment

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

great job making use of javascript closure.
again you could make use of the ES6 syntax to clean up this function

const counter = () => {
  let count = 0;
  return () => ++count; 
}

};
const newCounter = counter();
console.log(newCounter());
console.log(newCounter());
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2


// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
};
let counter = 0;
object = {
increment: function(){
return counter++;
},
decrement: function(){
return counter--;
}

}
};