-
Notifications
You must be signed in to change notification settings - Fork 2.3k
v1 #397
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?
v1 #397
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 |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c | |
| {"id":7,"first_name":"Kathie","last_name":"Majury","email":"[email protected]","shirt_size":"3XL","company_name":"Zoomcast","donation":261}, | ||
| {"id":8,"first_name":"Tanner","last_name":"Branton","email":"[email protected]","shirt_size":"2XL","company_name":"Realmix","donation":28}, | ||
| {"id":9,"first_name":"Sarina","last_name":"Lasham","email":"[email protected]","shirt_size":"XL","company_name":"Gigashots","donation":110}, | ||
| {"id":10,"first_name":"Bertie","last_name":"Lonergan","email":"[email protected]","shirt_size":"3XL","company_name":"Skinte","donation":62}, | ||
| {"id":10,"first_name":"Bertie","last_name":"Lonergan","email":"blonergan9@issuu. com","shirt_size":"3XL","company_name":"Skinte","donation":62}, | ||
| {"id":11,"first_name":"Trevor","last_name":"Studd","email":"[email protected]","shirt_size":"S","company_name":"Cogidoo","donation":76}, | ||
| {"id":12,"first_name":"Malachi","last_name":"Okeshott","email":"[email protected]","shirt_size":"M","company_name":"DabZ","donation":91}, | ||
| {"id":13,"first_name":"Berget","last_name":"Logsdail","email":"[email protected]","shirt_size":"M","company_name":"Mymm","donation":9}, | ||
|
|
@@ -55,22 +55,30 @@ 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 = []; | ||
| let fullName = runners.map(function(item) { | ||
| return `${item.first_name} ${item.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); | ||
| let allCaps = runners.map(function(item) { | ||
| return item[i].first_name.toUpperCase(); | ||
|
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. No need for |
||
| }); | ||
| 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 = []; | ||
| let largeShirts = runners.filter(function(item) { | ||
| return item.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((total, item) => total += item.donation, 0); | ||
|
|
||
| console.log(ticketPriceTotal); | ||
|
|
||
| // ==== Challenge 5: Be Creative ==== | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| // Create a callback function and invoke the 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']; | ||
| function firstItem(arr, cb) { | ||
| return cb(arr[0]); | ||
| }; | ||
| firstItem(items, function(first) { | ||
| console.log(first); | ||
| }) | ||
|
|
||
| /* | ||
|
|
||
|
|
@@ -24,23 +30,38 @@ 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(item, function(arrayLength) { | ||
|
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. No need for that line of code. |
||
| console.log(arrayLength) | ||
| }) | ||
|
|
||
| } | ||
|
|
||
| function last(arr, cb) { | ||
| // last passes the last item of the array into the callback. | ||
| return cb(arr.length-1) | ||
|
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. Should be |
||
| } | ||
|
|
||
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| return cb(x+y); | ||
| sumNums(function(sum) { | ||
| console.log(sum); | ||
| }) | ||
| } | ||
|
|
||
| function multiplyNums(x, y, cb) { | ||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| return cb(x*y); | ||
| sumNums(function(product) { | ||
| console.log(product); | ||
| }) | ||
| } | ||
|
|
||
| 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. | ||
|
|
||
| } | ||
|
|
||
| /* STRETCH PROBLEM */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| // ==== Challenge 1: Write your own closure ==== | ||
| // Write a simple closure of your own creation. Keep it simple! | ||
|
|
||
| function mathProb() { | ||
| let x = 3; | ||
| let y = 2; | ||
| return x+y; | ||
| } | ||
|
|
||
| // ==== Challenge 2: Create a counter function ==== | ||
| const counter = () => { | ||
| let counterVar = 0 += x; | ||
|
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. This is invalid left hand side assignment. |
||
| return counterVar; | ||
| // Return a function that when invoked increments and returns a counter variable. | ||
| }; | ||
| // Example usage: const newCounter = counter(); | ||
|
|
||
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.
forEachwas supposed to be used here instead ofmap.