MYs Flashcards

1
Q

myFind(array, callback)

A
function myFind (array, callback) {
  for (let i = 0; i < array.length; i++) {
    if (callback(array[i])) {
      return array[i];
    }
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

mySome

A
Array.prototype.mySome = function (callback) {
  let some = false;

this.myEach(el => {
if (callback(el)) some = true;
});

return some;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

myRotate

A
Array.prototype.myRotate = function (times = 1) {
  let rotations;
  const rotated = this.slice(0);
  if (times < 0) {
    rotations = this.length + (times % this.length);
  } else {
    rotations = times % this.length;
  }
  for (let i = 0; i < rotations; i++) {
    rotated.push(rotated.shift());
  }

return rotated;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

mySlice

A
String.prototype.mySlice = function(start, end) {
  let slice = "";

if (typeof end === ‘undefined’) {
end = this.length;
}

  for (let i = start; i < end && i < this.length; i++) {
    slice += this[i];
  }
  return slice;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

myReverse(array)

A
function myReverse(array) {
  const result = [];
  for (let i = 1; i < array.length + 1; i++) {
    result[i - 1] = array[array.length - i];
  }

return result;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

myReduce

A
Array.prototype.myReduce = function (callback, acc) {
  const array = this.slice();
  if (typeof acc === 'undefined') {
    acc = array.shift();
  }

array.myEach(el => {
acc = callback(acc, el);
});

return acc;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

myReject

A
Array.prototype.myReject = function (callback) {
  const selection = [];
  this.myEach(el => {
    if (!callback(el)) {
      selection.push(el);
    }
  });

return selection;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

myJoin

A
Array.prototype.myJoin = function (separator = '') {
  let newString = '';

this.forEach( (el, idx) => {
newString += ${el};
if (idx < this.length - 1) newString += separator;
});

return newString;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

myFlatten

A
Array.prototype.myFlatten = function () {
  let flattened = [];
  this.forEach((el) => {
    if (el instanceof Array) {
      flattened = flattened.concat(el.myFlatten());
    } else {
      flattened.push(el);
    }
  });

return flattened;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

myFilter

A
Array.prototype.myFilter = function (callback) {
  const result = [];

this.myEach((el) => {
if (callback(el)) result.push(el)
});

return result;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

myEvery

A
Array.prototype.myEvery = function (callback) {
  let every = true

this.myEach(el => {
if (!callback(el)) every = false;
});

return every;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

myEach

A
Array.prototype.myEach = function (func) {
  for (let i = 0; i < this.length; i++) {
    func(this[i]);
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

myCurry

A
Function.prototype.myCurry = function (numArgs) {
  let nums = [];
  let fcn = this;
  return function _myCurry (el) {
    nums.push(el);
    if (nums.length < numArgs) {
      return _myCurry;
    } else {
      return fcn(...nums);
    }
  };
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

myBind

A
Function.prototype.myBind = function (context, ...bindArgs) {
  const that = this;
  return function (...callArgs) {
    return that.apply(context, bindArgs.concat(callArgs));
  };
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

myCall

A

Function.prototype.myCall = function (context, …args) {
return this.bind(context)(…args);
};

// Also works (passing arguments at bind-time vs call-time)
Function.prototype.myCall = function (context, ...args) {
  return this.bind(context, ...args)();
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

myApply

A

Function.prototype.myApply = function (context, args = []) {
return this.bind(context)(…args);
};

// Also works (passing arguments at bind-time vs call-time)
Function.prototype.myApply = function (context, args = []) {
  return this.bind(context, ...args)();
};