-
Notifications
You must be signed in to change notification settings - Fork 0
javascript 2 #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
base: master
Are you sure you want to change the base?
javascript 2 #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
| }) | ||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not necessary to do |
||
| 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`) | ||
| }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another good example of map. 👍 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| } | ||
|
|
@@ -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) | ||
| }); | ||
|
|
@@ -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)}) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great play here with |
||
| ) | ||
| } | ||
| contains('Pencil', items, function verify(result) {console.log(result)}) | ||
| /* STRETCH PROBLEM */ | ||
|
|
||
| function removeDuplicates(array, cb) { | ||
|
|
||
| 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
|
||
There was a problem hiding this comment.
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
mapcreates a new array for you, and pushes elements into it based on the result of the callback. 💯