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
33 changes: 27 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,51 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"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.
// 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 = [];
runners.forEach(function(names){
fullName.push(`${names.first_name} ${names.last_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 = [];
console.log(allCaps);
allCaps = runners.map(function(names)
{
return names.first_name.toUpperCase()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done! You're taking advantage of the fact that map creates a new array for you, and pushes elements into it based on the result of the callback. 💯

})
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(runner => runner.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 = [];

let ticketPriceTotal = runners.reduce((totaldonations, runner) => {return totaldonations += runner.donation;}, 0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's not necessary to do += here: it's good enough to say totalDonations + runner.donation. The reason being is that whatever the result of the callback function is, reduce will automatically make it to be the new value for totalDonations, which it will then pass in the next time it calls the callback function, or return if it's already done.

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 - return details of people with donation over 100
let highdonation =[]
highdonation = runners.filter(runner => runner.donation > 100)
console.log(highdonation)


// Problem 2
// Problem 2 array of all company names
let companyNames = []
companyNames = runners.map((runner, index) => {return runner.company_name})
console.log(companyNames)

// Problem 3
// Problem 3 all company names with "inc." added to their name
let newCompanyNames = []
newCompanyNames = runners.map(function(company_name){
return (`${company_name} inc`)
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Another good example of map. 👍

24 changes: 18 additions & 6 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

/*
/*

//Given this problem:

//Given this problem:

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
}
Expand All @@ -17,7 +17,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
return cb(arr[0]);
}

// Function invocation
// Function invocation
firstItem(items, function(first) {
console.log(first)
});
Expand All @@ -27,25 +27,37 @@ 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, function(length){
console.log(length)})


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

Choose a reason for hiding this comment

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

You consistently pass a callback function which logs to the console whatever value is passed into it. While you change the name of the parameter on each one, it is essentially the same function.

Remember, you can use functions like they're values, and save them to variables. You can then use that variable to refer to that function:

function consoleLog(input) {
    console.log(input);
}

getLength(items, consoleLog);
last(items, consoleLog);
sumNums(x, y, consoleLog);


function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x+y)
}
sumNums(2,4, function sum(sumof){console.log(sumof)})

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

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 (console.log(list.includes(item))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great play here with includes!

)
}
contains('Pencil', items, function verify(result) {console.log(result)})
/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
8 changes: 8 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
const fn = "Sachin";
function fullname()
{
const ln = "Benny";
console.log(`${fn} ${ln}`);
}
fullname();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remember, you don't need to have a variable in the global scope to have a closure. Something like this would work too:

function fullName() {
    const fn = "Sachin";

    return function sayName() {
        const ln = "Benny";
        return `${fn} ${ln}`;
    }
}

const sayName = fullName();
console.log(sayName()); // Sachin Benny




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