Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. Make list drill
  2.  
  3. function makeList(item1, item2, item3) {
  4. const arr = [item1, item2, item3];
  5. return arr;
  6. }
  7.  
  8.  
  9. Add to list drill
  10.  
  11. function addToList(list, item) {
  12. list.push(item);
  13. return list;
  14. }
  15.  
  16.  
  17. Access first and third items
  18.  
  19. function accessFirstItem(array) {
  20. return (array[0]);
  21. }
  22.  
  23. function accessThirdItem(array) {
  24. return (array[2]);
  25. }
  26.  
  27.  
  28. Array length and access
  29.  
  30. function findLength(array) {
  31. return (array.length);
  32. }
  33.  
  34. function accessLastItem(array) {
  35. return array.pop();
  36. }
  37.  
  38.  
  39. Array copying I
  40.  
  41. function firstFourItems(array) {
  42. return array.slice(0, 4);
  43. }
  44.  
  45. function lastThreeItems(array) {
  46. return array.slice(-3);
  47. }
  48.  
  49.  
  50. Array copying II
  51.  
  52. function minusLastItem(array) {
  53. const lengthMinusOne = array.length - 1
  54. return array.slice(0, lengthMinusOne)
  55. }
  56.  
  57. function copyFirstHalf(array) {
  58. const halfArray = (array.length / 2)
  59. return array.slice(0, halfArray)
  60. }
  61.  
  62.  
  63. Squares with map
  64.  
  65. function squares(array) {
  66. function squared(num) {
  67. return (num ** 2);
  68. }
  69. return array.map(squared);
  70. }
  71.  
  72.  
  73. Sort greatest to least
  74.  
  75. function greatestToLeast(array) {
  76. function sortGreatest(a, b){
  77. return b-a;
  78. } return array.sort(sortGreatest);
  79. }
  80.  
  81.  
  82. Filter
  83.  
  84. function shortWords(array) {
  85. function lessThanFive(num) {
  86. return num.length < 5;
  87. } return array.filter(lessThanFive)
  88. }
  89.  
  90.  
  91. Find
  92.  
  93. function divisibleBy5(array) {
  94. function divisible(num) {
  95. return num % 5 === 0;
  96. } return array.find(divisible);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement