Guest User

Untitled

a guest
Mar 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. Array Cardio Day 1
  2.  
  3. // Array.prototype.filter()
  4. // 1. Filter the list of inventors for those who were born in the 1500's
  5. const fifteen = inventors.filter(inventor => {
  6. if(inventor.year >= 1500 && inventor.year < 1600){
  7. return true
  8. }
  9. })
  10.  
  11. // Array.prototype.map()
  12. // 2. Give us an array of the inventors' first and last names
  13. inventors.map(inventor => {
  14. return inventor.first + " " + inventor.last
  15. })
  16.  
  17. // Array.prototype.sort()
  18. // 3. Sort the inventors by birthdate, oldest to youngest
  19. inventors.sort((a, b) => a.year > b.year ? 1 : -1)
  20.  
  21. // 5. Sort the inventors by years lived
  22. const inventorVitality = inventors.sort((a, b) => {
  23. lastGuy = a.passed - a.year
  24. nextGuy = b.passed - b.year
  25. return lastGuy > nextGuy ? 1 : -1
  26. })
  27.  
  28.  
  29. // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
  30. const category = document.querySelector('.mw-category')
  31. const links = [...(category.querySelectorAll('a'))]
  32.  
  33. const de = links
  34. .map(link => link.textContent)
  35. .filter(srteetName => srteetName.includes('de'))
  36.  
  37. // 7. sort Exercise
  38. // Sort the people alphabetically by last name
  39. const sortingPeople = people.sort((lastOne, nextOne) => {
  40. const [aLast, aFirst] = lastOne.split(", ");
  41. const [bLast, bFirst] = nextOne.split(", ");
  42. return aLast > bLast ? 1 : -1
  43. })
  44.  
  45. // 8. Reduce Exercise
  46. // Sum up the instances of each of these
  47. const transporting = data.reduce((obj, item) => {
  48. if (!obj[item]) {
  49. obj[item] = 0;
  50. }
  51. obj[item]++;
  52. return obj;
  53. }, {});
  54.  
  55.  
  56. Array Cardio Day 2
  57.  
  58. // Array.prototype.some() // is at least one person 19 or older
  59. people.some(person => {
  60. var currentYear = (new Date()).getFullYear();
  61. return currentYear - person.year
  62. })
  63.  
  64. // Array.prototype.every() // is everyone 19 or older?
  65. people.every(person => {
  66. ( (new Date()).getFullYear() ) - person.year >= 19
  67. })
  68.  
  69. // Array.prototype.find()
  70. // Find is like filter, but instead returns just the one you are looking for
  71. // find the comment with the ID of 823423
  72. comments.find(comment => comment.id === 823423 )
  73.  
  74. // Array.prototype.findIndex()
  75. // Find the comment with this ID
  76. const index = comments.findIndex(comment => comment.id === 823423)
Add Comment
Please, Sign In to add comment