Guest User

Untitled

a guest
Mar 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. //forEach
  2.  
  3. Array.prototype.myforEach = function(cb) {
  4. for (let i = 0; i < this.length; i++){
  5. cb(this[i], i, this);
  6. }
  7. };
  8.  
  9.  
  10.  
  11.  
  12. //map
  13.  
  14. Array.prototype.myMap = function(cb) {
  15. let newArr = [];
  16. for (let i = 0; i < this.length; i++){
  17. newArr.push(cb(this[i], i, this));
  18. }
  19. return newArr;
  20. };
  21.  
  22.  
  23.  
  24.  
  25. //filter
  26.  
  27. Array.prototype.myFilter = function(cb) {
  28. let newArr = [];
  29. for (let i = 0; i < this.length; i++) {
  30. if (cb(this[i], i, this)){
  31. newArr.push(this[i]);
  32. }
  33. }
  34. return newArr;
  35. };
  36.  
  37.  
  38.  
  39.  
  40. //some
  41.  
  42. Array.prototype.mySome = function(cb) {
  43. for (let i = 0; i < this.length; i++) {
  44. if (cb(this[i], i, this)){
  45. return true;
  46. }
  47. };
  48. return false;
  49. };
  50.  
  51.  
  52.  
  53.  
  54.  
  55. //every
  56.  
  57. Array.prototype.myEvery = function(cb) {
  58. for (let i = 0; i < this.length; i++) {
  59. if (!cb(this[i], i, this)){
  60. return false;
  61. }
  62. };
  63. return true;
  64. };
  65.  
  66.  
  67.  
  68. //reduce
  69.  
  70. Array.prototype.myReduce = function(cb, initialVal) {
  71. var accumulator = (initialVal === undefined) ? undefined : initialVal;
  72. for (var i = 0; i < this.length; i++) {
  73. if (accumulator !== undefined){
  74. accumulator = cb.call(undefined, accumulator, this[i], i, this);
  75. } else {
  76. accumulator = this[i];
  77. }
  78. }
  79. return accumulator;
  80. };
Add Comment
Please, Sign In to add comment