Repository navigation
Aislynn edmiston #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?
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 |
|---|---|---|
|
|
@@ -56,28 +56,97 @@ 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 = []; | ||
|
|
||
| runners.forEach(function firstAndLastName(element) { | ||
| let i = element.first_name + " " + element.last_name; | ||
| fullName.push(i); | ||
| }); | ||
|
|
||
|
|
||
| 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 = []; | ||
|
|
||
| runners.map(function (runners){ | ||
| allCaps.push(`${runners.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 = []; | ||
|
|
||
| runners.filter(function(runner){ | ||
| if (`${runner.shirt_size}` === 'L'){ | ||
| largeShirts.push(runner); | ||
| } | ||
| }) | ||
|
|
||
| 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); | ||
|
|
||
| runners.forEach(function(runner){ | ||
| ticketPriceTotal.push(runner.donation); | ||
| }) | ||
|
|
||
| console.log(ticketPriceTotal.reduce(function (pv, cv, ci, arr){ | ||
|
Collaborator
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. Nice work! |
||
| return pv + cv; | ||
| })); | ||
|
|
||
| // ==== 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 - In a new array filter the runners and their ids to help identify them as they cross the finish line. | ||
|
|
||
| let idAndName = []; | ||
|
|
||
| runners.map(function(runner){ | ||
| idAndName.push(`Runner number ${runner.id} is ${runner.first_name} ${runner.last_name}`); | ||
| }) | ||
|
|
||
| console.log(idAndName); | ||
|
|
||
| // Problem 2 - Make arrays that are based on the shirt sizes for the runners. m, s, 2xl, l, xl, xs, 3xl, | ||
|
|
||
| let xtraSmall = []; | ||
| let small = []; | ||
| let medium = []; | ||
| let large = []; | ||
| let xtraLarge = []; | ||
| let twoXlarge = []; | ||
|
|
||
| runners.filter(function(runner){ | ||
| if(`${runner.shirt_size}` === 'XS'){ | ||
| xtraSmall.push(`XtraSmall: ${runner.first_name} ${runner.last_name}`) | ||
| } else if (`${runner.shirt_size}` === 'S'){ | ||
| small.push(`Small: ${runner.first_name} ${runner.last_name}`) | ||
| } else if (`${runner.shirt_size}` === 'M'){ | ||
| medium.push(`Medium: ${runner.first_name} ${runner.last_name}`) | ||
| } else if (`${runner.shirt_size}` === 'L'){ | ||
| large.push(`Large: ${runner.first_name} ${runner.last_name}`) | ||
| } else if (`${runner.shirt_size}` === 'XL'){ | ||
| xtraLarge.push(`XtraLarge: ${runner.first_name} ${runner.last_name}`) | ||
| } else if (`${runner.shirt_size}` === '2XL'){ | ||
| twoXlarge.push(`2XtraLarge: ${runner.first_name} ${runner.last_name}`) | ||
| } | ||
| }) | ||
|
|
||
| console.log(xtraSmall, small, medium, large, xtraLarge, twoXlarge); | ||
|
|
||
|
|
||
| // Problem 3 - Sort the runners in alphabetical order. | ||
|
|
||
| let sorted = []; | ||
|
|
||
| runners.map(function(runner){ | ||
| sorted.push(`${runner.first_name} ${runner.last_name}`); | ||
|
|
||
| // Problem 2 | ||
| }) | ||
|
|
||
| // Problem 3 | ||
| console.log(sorted.sort()); | ||
|
Collaborator
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. Nice work on these three problems! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,25 +24,49 @@ 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(first){ | ||
|
Collaborator
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. The naming of the argument "first" is a bit confusing here. |
||
| console.log(first); | ||
| }); | ||
|
|
||
| function last(arr, cb) { | ||
| // last passes the last item of the array into the callback. | ||
| return cb(arr.slice(-1).pop()); | ||
|
Collaborator
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. Nice! You can also use |
||
| } | ||
|
|
||
| last(items, function(first) { | ||
| console.log(first) | ||
| }); | ||
|
|
||
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| return cb(x + y); | ||
| } | ||
|
|
||
| sumNums(6, 48, function(add) { | ||
| console.log(add) | ||
| }); | ||
|
|
||
| function multiplyNums(x, y, cb) { | ||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| return cb(x*y); | ||
| } | ||
|
|
||
| multiplyNums(6, 3, function(multiply) { | ||
| console.log(multiply) | ||
| }); | ||
|
|
||
|
|
||
| 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 cb (list.includes(item)); | ||
| } | ||
|
|
||
| contains( 'rock', items, console.log); | ||
|
Collaborator
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. Nice work on all these callbacks! |
||
|
|
||
| /* STRETCH PROBLEM */ | ||
|
|
||
| function removeDuplicates(array, cb) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,27 @@ | ||
| // ==== Challenge 1: Write your own closure ==== | ||
| // Write a simple closure of your own creation. Keep it simple! | ||
|
|
||
| function dateOfBirth(month, day, year){ | ||
|
Collaborator
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 a great start. I noticed your console.log is a bit off and you're not using myBirth, though. Here's how it looks with those fixed: |
||
| const myBirth = "The date of my birth is:"; | ||
|
|
||
| function myBirthday (){ | ||
| return `${month} ${day}, ${year}`; | ||
| } | ||
|
|
||
| return myBirthday(); | ||
| } | ||
|
|
||
| console.log("May", "15", '1993'); | ||
|
|
||
| // ==== Challenge 2: Create a counter function ==== | ||
| const counter = () => { | ||
| // Return a function that when invoked increments and returns a counter variable. | ||
| let count = 0; | ||
| return () => (++count); | ||
|
Collaborator
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. Very sleek ES6! |
||
| }; | ||
| // Example usage: const newCounter = counter(); | ||
| // newCounter(); // 1 | ||
| // newCounter(); // 2 | ||
| const newCounter = counter(); | ||
| console.log(newCounter()); // 1 | ||
| console.log(newCounter()); // 2 | ||
|
|
||
| /* STRETCH PROBLEM, 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.
I'd recommend naming "element" what it is so the code is more semantic. In this case, each element is a "runner." Similarly, your variable "i" could be called "names" so that the logic is a bit easier to follow. Typically, "i" refers to an index.