Guest User

Untitled

a guest
Jan 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. function each(array, f) {
  2. for (var i = 0; i < array.length; i++) {
  3. f(array[i]);
  4. }
  5. }
  6.  
  7. function map(array, f) {
  8. var acc = [];
  9. each(array, function(element) {
  10. acc.push(f(element));
  11. });
  12. return acc;
  13. }
  14.  
  15. function reduce(array, f, start) {
  16. var acc = start;
  17. each(array, function(element) {
  18. acc = f(acc, element);
  19. });
  20. return acc;
  21. }
  22.  
  23. // function product(numbers) {
  24. // return reduce(numbers, function(acc, number) {
  25. // return acc * number;
  26. // }, 1);
  27. // }
  28.  
  29. // console.log(product([1, 2, 3, 4]));
  30.  
  31. // var people = [
  32. // {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26},
  33. // {name: {first: "Ben", last: "Bitdiddle"}, age: 34},
  34. // {name: {first: "Eva", middle: "Lu", last: "Ator"}, age: 40},
  35. // {name: {first: "Lem", middle: "E.", last: "Tweakit"}, age: 45},
  36. // {name: {first: "Louis", last: "Reasoner"}, age: 21}
  37. // ];
  38.  
  39. // function averageAge(people) {
  40. // var ages = map(people, function(person) {
  41. // return person.age;
  42. // });
  43. // console.log(ages);
  44. // return reduce(ages, function(acc, age) {
  45. // return acc += age;
  46. // }, 0) / ages.length;
  47. // }
  48.  
  49. // console.log(averageAge(people));
  50.  
  51. // function range(start, end) {
  52. // var acc = [];
  53. // for (var i = start; i < end; i++) {
  54. // acc.push(i);
  55. // }
  56. // return acc;
  57. // }
  58. // console.log(range(1, 5));
  59. // function factorial(n) {
  60. // return reduce(range(1, n + 1), function(acc, element) {
  61. // return acc *= element;
  62. // }, 1);
  63. // }
  64.  
  65. // console.log(factorial(4));
  66.  
  67. // var arr = [1, 2, 3]
  68. // function sumBy(numbers, f) {
  69. // return reduce(numbers, function(sum, number) {
  70. // return sum += f(number);
  71. // }, 0);
  72. // return sum;
  73. // }
  74.  
  75. // function square(x) {
  76. // return x * x;
  77. // }
  78.  
  79. // function cube(x) {
  80. // return x * x * x;
  81. // }
  82.  
  83. //sumBy(arr, square); // => 30
  84. //Note: you will have to create a function "square" that returns the square of an input number.
  85. //sumBy(arr, cube); // => 100
  86. //Note: you will have to create a function "cube" that returns the cube of an input number.
  87.  
  88.  
  89. // function max(numbers) {
  90. // var max = numbers[0];
  91. // each(numbers, function(number) {
  92. // if (number > max) {
  93. // max = number;
  94. // }
  95. // });
  96. // return max;
  97. // }
  98.  
  99. // function max(numbers) {
  100. // return reduce(numbers, function(max, number) {
  101. // console.log(number, max);
  102. // if(number > max) {
  103. // return max = number;
  104. // }
  105. // }, numbers[0]);
  106. // }
  107. // console.log(max([1, 2, 3, 4, 5]));
Add Comment
Please, Sign In to add comment