mymap.js 814 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. let a = [1, 2, 3, 4, 5];
  2. let f = (x) => x * x;
  3. var myMap = function (arr, func) {
  4. let arrTemp = [];
  5. for (item of arr) {
  6. arrTemp.push(func(item));
  7. }
  8. return arrTemp;
  9. };
  10. myMap(a, f);
  11. console.log(myMap(a, f));
  12. //-----------------------------
  13. let b = [3, -2, 50, 0, 33];
  14. let ff = (x) => x > 10;
  15. var myFilter = function (arr, func) {
  16. let arrTemp = [];
  17. for (item of arr)
  18. if (func(item)) {
  19. arrTemp.push(item);
  20. }
  21. return arrTemp;
  22. };
  23. let bb = myFilter(b, ff);
  24. console.log(bb);
  25. //-----------------------------
  26. let b = [1, 2, 3, 4];
  27. let rr = (a, b) => a + b;
  28. var myReduce = function (arr, func) {
  29. for (i = 1; i < arr.length; i++) {
  30. arr[i] = func(arr[i - 1], arr[i]);
  31. }
  32. return arr[arr.length - 1];
  33. };
  34. console.log(myReduce(b, rr));