1. If you

const debounce = (fn, ms = 0) = > {
  let timeoutId;
  return function(. args) {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() = > fn.apply(this, args), ms);
  };
};
Copy the code

2. The throttle

const throttle = (fn, time) = > {
  let flag = true;
  return function() {
    if(! flag)return;
    flag = false;
    setTimeout(() = > {
      fn.apply(this.arguments);
      flag = true; }, time); }}Copy the code

3. Function creatization

const curry = (fn, arity = fn.length, ... args) = >arity <= args.length ? fn(... args) : curry.bind(null, fn, arity, ... args);// Examples
curry(Math.pow)(2) (10); / / 1024
curry(Math.min, 3) (10) (50) (2); / / 2
Copy the code

4. A deep clone

const deepClone = obj= > {
  if (obj === null) return null;
  let clone = Object.assign({}, obj);
  Object.keys(clone).forEach(
    key= >
      (clone[key] =
        typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
  );
  if (Array.isArray(obj)) {
    clone.length = obj.length;
    return Array.from(clone);
  }
  return clone;
};
Copy the code

5. Array flattening

const deepFlatten = arr= >[].concat(... arr.map(v= > (Array.isArray(v) ? deepFlatten(v) : v)));
  
// Examples
deepFlatten([1[2], [[3].4].5]); // [1, 2, 3, 4, 5]
Copy the code

6. Quicksort

const quickSort = arr= > {
  const a = [...arr];
  if (a.length < 2) return a;
  const pivotIndex = Math.floor(arr.length / 2);
  const pivot = a[pivotIndex];
  const [lo, hi] = a.reduce(
    (acc, val, i) = > {
      if(val < pivot || (val === pivot && i ! = pivotIndex)) { acc[0].push(val);
      } else if (val > pivot) {
        acc[1].push(val);
      }
      returnacc; }, [[], []]);return [...quickSort(lo), pivot, ...quickSort(hi)];
};

// Examples
quickSort([1.6.1.5.3.2.1.4]); // [1, 1, 1, 2, 3, 4, 5, 6]
Copy the code

7. Bubble sort

const bubbleSort = arr= > {
  let swapped = false;
  const a = [...arr];
  for (let i = 1; i < a.length - 1; i++) {
    swapped = false;
    for (let j = 0; j < a.length - i; j++) {
      if (a[j + 1] < a[j]) {
        [a[j], a[j + 1]] = [a[j + 1], a[j]];
        swapped = true; }}if(! swapped)return a;
  }
  return a;
};

// Examples
bubbleSort([2.1.4.3]); // [1, 2, 3, 4]
Copy the code

8. Merge sort

const mergeSort = arr= > {
  if (arr.length < 2) return arr;
  const mid = Math.floor(arr.length / 2);
  const l = mergeSort(arr.slice(0, mid));
  const r = mergeSort(arr.slice(mid, arr.length));
  return Array.from({ length: l.length + r.length }, () = > {
    if(! l.length)return r.shift();
    else if(! r.length)return l.shift();
    else return l[0] > r[0]? r.shift() : l.shift(); }); };// Examples
mergeSort([5.1.4.2.3]); // [1, 2, 3, 4, 5]
Copy the code

9. Insert sort

const insertionSort = arr= >
  arr.reduce((acc, x) = > {
    if(! acc.length)return [x];
    acc.some((y, j) = > {
      if (x <= y) {
        acc.splice(j, 0, x);
        return true;
      }
      if (x > y && j === acc.length - 1) {
        acc.splice(j + 1.0, x);
        return true;
      }
      return false;
    });
    returnacc; } []);// Examples
insertionSort([6.3.4.1]); // [1, 3, 4, 6]
Copy the code

10. Select sort

const selectionSort = arr= > {
  const a = [...arr];
  for (let i = 0; i < a.length; i++) {
    const min = a
      .slice(i + 1)
      .reduce((acc, val, j) = > (val < a[acc] ? j + i + 1 : acc), i);
    if(min ! == i) [a[i], a[min]] = [a[min], a[i]]; }return a;
};

// Examples
selectionSort([5.1.4.2.3]); // [1, 2, 3, 4, 5]
Copy the code

11. To realize the new

function myNew(fn, ... args) {
  let obj = Object.create(fn.prototype);
  letres = fn.call(obj, ... args);if (res && (typeof res === "object" || typeof res === "function")) {
    return res;
  }
  return obj;
}
Copy the code

reference

The most complete handwritten JS interview questions