0. Small Hao algorithm


/ / the bubbling
const array = [23.3.12.44.42.344];
function bubbleSort(arr) {
    const len = arr.length;
    for (let i = 0; i < len - 1; i++) {
        for (let j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                const temp = arr[j + 1];
                arr[j + 1] = arr[j]; arr[j] = temp; }}}return arr;
}
bubbleSort(array);

/ / select
function selectSort(arr) {
    const len = arr.length;
    let minIndex;
    let temp;
    for (let i = 0; i < len - 1; i++) {
        minIndex = i;
        for (let j = i + 1; j < len; j++) {
            if (arr[minIndex] > arr[j]) {
                minIndex = j;
            }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
    return arr;
}
selectSort(array);

/ / insert
function insertSort(arr) {
    const len = arr.length;
    for (let i = 1; i < len; i++) {
        preIndex = i - 1;
        current = arr[i];
        while (preIndex >= 0 && arr[preIndex] > current) {
            arr[preIndex + 1] = arr[preIndex];
            preIndex--;
        }
        arr[preIndex + 1] = current;
    }
    return arr;
}
insertSort(array);

/ / hill
const aaaa = [23.34.2.2.13.2.45];
function insertion(arr) {
    if (arr === null || arr.length <= 1) {
        return arr;
    }
    const len = arr.length;
    for (let d = Math.floor(len / 2); d > 0; d = Math.floor(d / 2)) {
        for (let i = d; i < len; i++) {
            for (let j = i - d; j >= 0; j -= d) {
                if (arr[j] > arr[j + d]) {
                    consttemp = arr[j + d]; arr[j + d] = arr[j]; arr[j] = temp; }}}}return arr;
}
insertion(aaaa);

/ / merge
const aaaaa = [233.3.2.2.232.23.2432.333];
function merge(left, right) {
    const result = [];
    const minIndex = Math.min(left.length, right.length);
    while (left.length && right.length) {
        if (left[0] <= right[0]) {
            result.push(left.shift());
        } else{ result.push(right.shift()); }}while (left.length) {
        result.push(left.shift());
    }
    while (right.length) {
        result.push(right.shift());
    }
    return result;
}

function mergeSort(arr) {
    if (arr === null || arr.length < 2) {
        return arr;
    }
    const middle = Math.floor(arr.length / 2);
    const left = arr.slice(0, middle);
    const right = arr.slice(middle);
    return merge(mergeSort(left), mergeSort(right));
}
mergeSort(aaaaa);

/ / fast
function swap(A, i, j) {
    [A[i], A[j]] = [A[j], A[i]];
}

function partition(arr,left,right) {
    //const pivot = A[hi - 1];
    //let i = lo;
    //let j = hi - 2;
    //while (i ! == j) {
    // A[i] <= pivot ? i++ : swap(A, i, j--);
    / /}
    //if (lo < hi - 2) {
    // swap(A, j, hi - 1);
    / /}
    //return j;
    var pivot = left,    // Set the pivot value
        index = pivot + 1;
    for (var i = index; i <= right; i++) {
        if (arr[i] < arr[pivot]) {
            swap(arr, i, index);
            index++;
        }        
    }
    swap(arr, pivot, index - 1);
    return index-1;
}

function qsort(A, lo = 0, hi = A.length) {
    if (hi - lo < 2) return null;
    const p = partition(A, lo, hi);
    qsort(A, lo, p);
    qsort(A, p + 1, hi);
    return A;
}
qsort([10.80.40.32.11.31.1.3.20]);

// The intersection of two arrays
function inter(arr1, arr2) {
    const obj = {};
    arr1.forEach((item) = > {
        if(! obj[item]) { obj[item] =1;
        } else{ obj[item]++; }});const result = [];
    arr2.forEach((item) = > {
        if(obj[item]) { result.push(item); obj[item]--; }});return result;
}
const intersect = new Set([4.9.4].filter((x) = > new Set(9.4.9.8.4).has(x)));
console.log(intersect);
inter([4.9.4], [9.4.9.8.4]);

// The longest public prefix
function longest(arr) {
    const point = arr.shift();
    let len = point.length;
    for (let j = 0; j < arr.length; j++) {
        for (let i = 0; i < point.length; i++) {
            if(arr[j][i] ! == point[i]) {if (i < len) {
                    len = i;
                }
                break; }}}return point.substr(0, len);
}
longest(['flower'.'flow'.'flight']);

// The best time to buy and sell stocks
function maxProfit(arr) {
    let result = 0;
    for (let i = 0; i < arr.length - 1; i++) {
        if (arr[i + 1] > arr[i]) {
            result += arr[i + 1] - arr[i]; }}return result;
}
maxProfit([7.1.5.3.6.4]);

// Invert the array
function reverse1(arr, i, j) {
    while(i < j) { [arr[i], arr[j]] = [arr[j], arr[i]]; i++; j--; }}function reverseArr(arr, k) {
    k %= arr.length;
    if (arr.length < 2) {
        return arr;
    }
    reverse1(arr, 0, arr.length - 1);
    reverse1(arr, 0, k - 1);
    reverse1(arr, k, arr.length - 1);
    return arr;
}
reverseArr([1.2.3.4.5.6.7].3);

// delete in place
function spliceArr(arr, k) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === k) {
            [arr[i], arr[arr.length - 1]] = [arr[arr.length - 1], arr[i]]; arr.length--; i--; }}return arr.length;
}
spliceArr([1.2.3.4.5.6.7].3);

/ / plus one
function addArr(arr) {
    for (let i = arr.length - 1; i >= 0; i--) {
        if (arr[i] < 9) {
            arr[i] = arr[i] + 1;
            break;
        } else {
            arr[i] = 0;
        }
        if (i === 0) {
            arr.unshift(1); }}return arr;
}
addArr([9.9.8.9]);

// The sum of the two numbers
function twoSum(arr, k) {
    const obj = {};
    for (let i = 0; i < arr.length; i++) {
        if (obj[k - arr[i]] === undefined) {
            obj[arr[i]] = i;
        } else {
            return[obj[k - arr[i]], i]; }}return [];
}
twoSum([2.7.11.15].9);

// The sum of three numbers
function threeSum(arr) {
    arr = arr.sort();
    const result = [];
    for (let i = 0; i < arr.length; i++) {
        // for(let j=i; j

        // }
        let k = i + 1;
        let j = arr.length - 1;
        const target = 0 - arr[i];
        if (arr[i] <= 0) {
            if (i === 0|| arr[i] ! == arr[i -1]) {
                while (k < j) {
                    if (arr[k] + arr[j] === target) {
                        result.push([arr[i], arr[k], arr[j]]);
                        while (k < j && nums[k] === nums[k + 1]) k++;
                        while (k < j && nums[j] === nums[j - 1]) j--;
                        k++;
                        j--;
                    } else if (arr[j] + arr[k] < target) {
                        k++;
                    } else {
                        j--;
                    }
                }
            }
        }
    }
    return result;
}
threeSum([-1.0.1.2, -1, -4]);

// zigzag transform
function convert(s, numRows) {
    if (numRows === 1) {
        return s;
    }
    let down = true;
    const resultArr = [];
    let resultIndex = 0;
    for (let i = 0; i < s.length; i++) {
        resultArr[resultIndex] = (resultArr[resultIndex] ?? ' ') + s[i];
        if(i ! = =0 && i % (numRows - 1) = = =0) { down = ! down; } resultIndex += down ?1 : -1;
    }
    return resultArr.join(' ');
}
convert('LEETCODEISHIRING'.3);


// Delete the NTH node from the list.
var removeNthFromEnd = function(head, n) {
    let prevNode = new ListNode(0);
    prevNode.next = head;
    let show =  prevNode;
    let fast =  prevNode;
    while(n--){
        fast = fast.next;
    }
    while(fast&&fast.next){
        show = show.next;
        fast = fast.next;
    }
    show.next = show.next.next;
    return prevNode.next;
};

Merge two ordered lists
var mergeTwoLists = function (l1, l2) {
    let prevNode = new ListNode(0);
    let result = prevNode;
    while (l1 && l2) {
        if (l1.val < l2.val) {
            prevNode.next = l1;
            l1 = l1.next;
        } else {
            prevNode.next = l2;
            l2 = l2.next;
        }
        prevNode = prevNode.next;
    }
    if (l1) {
        prevNode.next = l1;
    }
    if (l2) {
        prevNode.next = l2;
    }
    return result.next;
};

// Loop list (141)
var hasCycle = function (head) {
    let slow = head;
    let fast = head;
    while (fast) {
        if(! fast.next)return false;
        slow = slow.next;
        fast = fast.next.next;
        if (slow === fast) return true;
    }
    return false;
};

// Add the two numbers
var addTwoNumbers = function (l1, l2) {
    let prevNode = new ListNode(0);
    let result = prevNode;
    let k = 0;
    while (l1 || l2 || k) {
        let num1 = l1 ? l1.val : 0;
        let num2 = l2 ? l2.val : 0;
        let count = num1 + num2 + k;
        result.next = new ListNode(count % 10);
        result = result.next;
        l1 && (l1 = l1.next);
        l2 && (l2 = l2.next);
        k = count > 9 ? 1 : 0;
    }
    return prevNode.next;
};

// Maximum suborder sum
var maxSubArray = function(nums) {
    if(nums.length<1) {return 0;
    }
    let dp = [];
	dp[0] = nums[0];
    let max = dp[0];
    for(let i=1; i<nums.length; i++){ dp[i] =Math.max(dp[i-1]+nums[i],nums[i]);
    if(dp[i]>max){ max = dp[i]; }}return max;
};

// 300. The longest increasing subsequence
var lengthOfLIS = function (nums) {
    let dp = new Array(nums.length).fill(1);
    if (nums.length < 1) {
        return 0;
    }
    for (let i = 1; i < nums.length; i++) {
        for (let j = 0; j < i; j++) {
            if (nums[i] > nums[j]) {
                dp[i] = Math.max(dp[i], dp[j] + 1); }}}return Math.max(... dp); };//120. Triangle minimum path sum
var minimumTotal = function(arr) {
    if(arr.length<1) {return 0;
    }
   let dp = Array.from(Array(arr.length),(item,index) = >new Array(index+1));
   dp[0] [0] = arr[0] [0];
   for(let i=1; i<arr.length; i++){for(let j=0; j<arr[i].length; j++){if(j===0){
               dp[i][j] = dp[i-1][j]+arr[i][j];
           }else if(j===arr[i].length-1){
               dp[i][j] = dp[i-1][j-1]+arr[i][j];
           }else{
               dp[i][j] = Math.min(dp[i-1][j-1],dp[i-1][j])+arr[i][j]; }}}return Math.min(... dp[dp.length-1])};//64. Minimum path sum
var minPathSum = function(grid) {
  let rows = grid.length;
    if(rows<1) {return 0;
    }
    let cols = grid[0].length;
    if(cols<1) {return 0;
    }
    let dp = Array(rows).fill(Array(cols).fill(0));
    
    for(let i=0; i<rows; i++){for(let j=0; j<cols; j++){if(i===0&&j===0){
                dp[0] [0] = grid[0] [0];
            }else if(i===0){
                dp[i][j] = dp[i][j-1]+grid[i][j];
            }else if(j===0){
                dp[i][j] = dp[i-1][j]+grid[i][j];
            }else{
                dp[i][j] = Math.min(dp[i-1][j]+grid[i][j],dp[i][j-1]+grid[i][j]); }}}return dp[rows-1][cols-1]};// 198
var rob = function(nums) {
   let dp = [];
    let len = nums.length;
    if(len<1) {return 0;
    }else if(len===1) {return nums[0];
    }else if(len===2) {return Math.max(nums[0],nums[1])
    }
    dp[0] = nums[0];
    dp[1] = Math.max(nums[0],nums[1]);
    for(let i=2; i<len; i++){ dp[i] =Math.max(dp[i-2]+nums[i],dp[i-1]);
    }
    return Math.max(... dp); };//344. Reverse the string
var reverseString = function(s) {
    if(s.length<=1) {return s;
    }
    let l = 0;
    let r = s.length-1;
    while(l<r){
        let temp = s[l];
        s[l] = s[r];
        s[r] = temp;
        l++;
        r--;
    }
    return s;
};

//387. The first unique character in a string
var firstUniqChar = function(s) {
    let obj = {};
    for(let i=0; i<s.length; i++){if(obj[s[i]]){
            obj[s[i]]++;
        }else{
            obj[s[i]] = 1; }}let newArr = Object.keys(obj);
    console.log(newArr,obj);
    for(let i=0; i<newArr.length; i++){if(obj[newArr[i]]===1) {return s.indexOf(newArr[i])
        }
    }
    return -1;
};

/ / implementation strStr ()
var strStr = function(str, target) {
    if(target===' ') {return 0;
    }
     for (let i = 0; i < str.length; i++) {
        if (str[i] === target[0]) {
            if (str.substr(i, target.length) === target) {
                returni; }}}return -1;
};

Print n digits from 1 to maximum
var printNumbers = function(n) {
    let l = 0;
    while(n){
        n--;
        l=10*l+9;
    }
    let result = [];
    for(let i=1; i<=l; i++){ result.push(i); }return result;
};


// 第125: Verify palindromes
var isPalindrome = function(str) {
    str = str.replace(/[^\w]|_/g.' ').toLowerCase();
if (str.length <= 1) return true;
    while (str.length >= 2) {
        if (str.substr(0.1) !== str.substr(str.length - 1)) {
            return false;
        }
        str = str.substring(1, str.length - 1);
    }
    return true;
};

// Problem 796: Rotate string
// Given two strings, A and B. The rotation operation of A moves the leftmost character of A to the rightmost. For example, if A = 'abcde',
// The result is 'bcdea' after one move. Returns True if A can become B after A number of rotations.
var rotateString = function (a, b) {
    a = a + a;
    if (a.indexOf(b) > -1 && a.length / 2 === b.length) {
        return true;
    }
    return false;
};

// Length of last word (58)
var lengthOfLastWord = function(words) {
    let result =words.match(/\s*(\w+)\s*$/i);
    if(result){
        result = result[1].length;
    }else{
        result = 0;
    }
    console.log(result);
    return result;
};


// Layer traversal with BFS(102)
var levelOrder = function(root) {
    return dfs(root,0[])};function dfs(root,level,res){
    if(! root){return res;
    }
    if(res.length===level){
        res.push([root.val])
    }else{
        res[level].push(root.val);
    }
    res = dfs(root.left,level+1,res);
    res = dfs(root.right,level+1,res);
    return res;
}

// 98: Verify binary search trees
var isValidBST = function(root) {
    return isBST(root,Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER);
};
function isBST(root,min,max){
    if(! root){return true;
    }
    if(min>=root.val||max<=root.val){
        return false;
    }
    return isBST(root.left,min,root.val) && isBST(root.right,root.val,max);
}

// 700. Search in binary search tree
var searchBST = function(root, val) {
    if(! root){return null;
    }
    if(val===root.val){
        return root;
    }else if(val<root.val){
        return searchBST(root.left,val);
    }else{
        returnsearchBST(root.right,val); }};// Problem 450: Delete a node in a binary search tree
const deleteNode = function (root, key) {
  if(root==null) {return root;
  }
  if(root.val<key){
      root.right = deleteNode(root.right,key);
  }else if(root.val>key){
      root.left = deleteNode(root.left,key);
  }else{
      if(root.left&&! root.right){ root = root.left; }else if(root.right&&! root.left){ root = root.right; }else if(! root.left&&! root.right){ root =null;
      }else{
          let last = root.left;
          while(last.right){ last = last.right; } root.val = last.val; root.left = deleteNode(root.left,last.val); }}return root;
}

//814. Binary tree pruning
var pruneTree = function(root) {
    return a(root);
};
function a(root){
    if(! root){return null;
    }
    root.left = a(root.left);
    root.right = a(root.right);
    if(! root.left&&! root.right&&! root.val){return null;
    }
    return root;
}

//46. Given a sequence without repeating digits, return all possible permutations.
var permute = function(nums) {
    let res = [];
    let used={};
    function dfs(path){
        if(path.length===nums.length){
            res.push(path.slice())
            return;
        }
        for(let num of nums){
            if(used[num])continue;
            used[num] = true;
            path.push(num);
            dfs(path);
            path.pop();
            used[num] = false;
        }

    }
    dfs([]);
    return res;
};

//239. Maximum sliding window value
var maxSlidingWindow = function(nums, k) {
    let queue = [];
    let result =[];
    for(let i=0; i<nums.length; i++){if(i-queue[0]>=k){
            queue.shift();
        }
        while(nums[queue[queue.length-1]]<=nums[i]){
            queue.pop();
        }
        queue.push(i)
        if(i>=k-1){
            result.push(nums[queue[0]])}}return result;
};


// The oldest string without repeating characters (3)
var lengthOfLongestSubstring = function(s) {
    let arr = [];
    let max = 0;
    for(let i=0; i<s.length; i++){let index = arr.indexOf(s[i])
        if(index>=0){
            arr.splice(0,index+1);
        }
        arr.push(s.charAt(i));
        max = Math.max(arr.length,max);
    }
    return max;

};
Copy the code

1. Use the re to parse the URL

function QueryString(item) {
    var sValue = location.search.match(new RegExp("[\? \ &]" + item + "= (*) [^ \ &] (\ &?) "."i"));
    return sValue ? sValue[1] : sValue;
}
// Other methods:
 let url = new URL('https://juejin.cn/editor/drafts/6940989915052113934?id=10&test=aa');
 url.searchParams.get('test');
Copy the code

2. Image lazy loading code

(function(){
    let images = document.querySelectorAll('img');
    let onloadImg = function(image){
        image.setAttribute('src',image.getAttribute('data-src'));
        image.addEventListener('load'.() = >{
            image.removeAttribute('data-src'); })}let IOb = new IntersectionObserver((items,observer) = >{
        console.log(items,43);
        items.forEach(item= >{
            if(item.isIntersecting){ onloadImg(item.target); observer.unobserve(item.target); }})}, {threshold:0.25});

    images.forEach(item= >{
        IOb.observe(item);
    })
})();

Copy the code

3. Priority queue

function queue() {
    this.data = [];
    this.enqueue = function (item) {
        this.data.push(item);
    };
    this.dequeue = function () {
        let maxIndex = 0;
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[maxIndex] < this.data[i]) { maxIndex = i; }}return this.data.splice(maxIndex, 1);
    };
}
queue();
Copy the code

4. Find all symmetries for [1000,990000]

function symmetry() {
    let result = [];
    for (let i = 10; i <= 988; i++) {
        let arrI = String(i).split(' ');
        let curI = [...arrI];
        let revI = [...arrI.reverse()];
        result.push(Number(curI.concat(revI).join(' ')));
    }
    return result;
}
symmetry();
Copy the code

5. Implement function A(), multiple calls output 1, 2, 1…

var A = (function () {
    let i = 1;
    return () = > {
        if (i % 2= = =0) {
            return i--;
        } else {
            return i++;
        }
    };
})();
A();
Copy the code

6. Write a publish/subscribe model


class myEventEmitter {
    constructor() {
        this.list = [];
    }
    on(type, cb) {
        this.list.push({ type, cb });
    }
    emit(type, message) {
        this.list.forEach((item) = > {
            if(item.type === type) { item.cb(message); }}); }}let eventEmitter = new myEventEmitter();
eventEmitter.on('msg'.(data) = > console.log(data));
eventEmitter.emit('msg'.'emit');
Copy the code

7. Given an array and a positive integer N, find the longest continuous subarray whose sum is less than N

function a(arr, num) {
    let maxLen = 0;
    let dp = Array.from(Array(arr.length), (item) = > Array(arr.length).fill(0));
    for (let i = 0; i < arr.length; i++) {
        dp[i][i] = arr[i];
        for (let j = i + 1; j < arr.length; j++) {
            dp[i][j] = dp[i][j - 1] + arr[j];
            if (dp[i][j] > num) {
                break;
            } else {
                if (j - i + 1 > maxLen) {
                    maxLen = j - i + 1; }}}}return maxLen;
}
a([2.345.3.223.23.5.34.56].500);
Copy the code

8. Single-line and multi-line truncation of CSS? (overflow, text-overflow)

div{
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
}
div{
    overflow:hidden;
    text-overflow:ellipsis;
    display:-webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
Copy the code

9. Bidirectional binding of Vue (event listeners, getters and setters)

var bValue = 38;
Object.defineProperty(o, 'b', {
    // Use method name abbreviations (ES2015 feature)
    get() {
        return bValue;
    },
    set(newValue) {
        bValue = newValue;
    },
    enumerable: true.configurable: true});Copy the code

10. Sum of paths (112)

// you are given the root node of the binary tree and an integer targetSum representing the targetSum. Determine if there is a path from the root node to the leaf node in the tree. The sum of the values of all nodes in the path equals the target and targetSum
var hasPathSum = function(root, targetSum) {
    if(! root){return false;
    }
    if(root.left===null&&root.right===null) {return (targetSum-root.val)===0
    }
    return hasPathSum(root.left,targetSum-root.val)||hasPathSum(root.right,targetSum-root.val);
};
Copy the code

11. Maximum and Minimum depth of binary tree (104) (111)


var maxDepth = function(root) {
    if(! root)return 0;
    const left = maxDepth(root.left);
    const right = maxDepth(root.right);
    return Math.max(left,right)+1;
};

var minDepth = function(root) {
    if(! root)return 0;
  
    if(! root.left){return Math.min(minDepth(root.right))+1;
    }
    if(! root.right){return Math.min(minDepth(root.left))+1;
    }
    const left = minDepth(root.left);
    const right= minDepth(root.right);
    return Math.min(left,right)+1;
};
Copy the code

12.Promise/A+ complete implementation

function Promise(executor) {
  let self = this;
  self.status = "pending";
  self.value = undefined;
  self.onResolvedCallbacks = [];
  self.onRejectedCallbacks = [];
  function resolve(value) {
    if (value instanceof Promise) {
      return value.then(resolve, reject)
    }
    setTimeout(function () { // Execute all callbacks asynchronously
      if (self.status == 'pending') {
        self.value = value;
        self.status = 'resolved';
        self.onResolvedCallbacks.forEach(item= >item(value)); }}); }function reject(value) {
    setTimeout(function () {
      if (self.status == 'pending') {
        self.value = value;
        self.status = 'rejected';
        self.onRejectedCallbacks.forEach(item= >item(value)); }}); }try {
    executor(resolve, reject);
  } catch(e) { reject(e); }}function resolvePromise(promise2, x, resolve, reject) {
  if (promise2 === x) {
    return reject(new TypeError('Circular reference'));
  }
  let then, called;

  if(x ! =null && ((typeof x == 'object' || typeof x == 'function'))) {
    try {
      then = x.then;
      if (typeof then == 'function') {
        then.call(x, function (y) {
          if (called)return;
          called = true;
          resolvePromise(promise2, y, resolve, reject);
        }, function (r) {
          if (called)return;
          called = true;
          reject(r);
        });
      } else{ resolve(x); }}catch (e) {
      if (called)return;
      called = true; reject(e); }}else{ resolve(x); }}Promise.prototype.then = function (onFulfilled, onRejected) {
  let self = this;
  onFulfilled = typeof onFulfilled == 'function' ? onFulfilled : function (value) {
    return value
  };
  onRejected = typeof onRejected == 'function' ? onRejected : function (value) {
    throw value
  };
  let promise2;
  if (self.status == 'resolved') {
    promise2 = new Promise(function (resolve, reject) {
      setTimeout(function () {
        try {
          let x = onFulfilled(self.value);
          resolvePromise(promise2, x, resolve, reject);
        } catch(e) { reject(e); }}); }); }if (self.status == 'rejected') {
    promise2 = new Promise(function (resolve, reject) {
      setTimeout(function () {
        try {
          let x = onRejected(self.value);
          resolvePromise(promise2, x, resolve, reject);
        } catch(e) { reject(e); }}); }); }if (self.status == 'pending') {
    promise2 = new Promise(function (resolve, reject) {
      self.onResolvedCallbacks.push(function (value) {
        try {
          let x = onFulfilled(value);
          resolvePromise(promise2, x, resolve, reject);
        } catch(e) { reject(e); }}); self.onRejectedCallbacks.push(function (value) {
        try {
          let x = onRejected(value);
          resolvePromise(promise2, x, resolve, reject);
        } catch(e) { reject(e); }}); }); }return promise2;
}
Promise.prototype.catch = function (onRejected) {
  return this.then(null, onRejected);
}
Promise.all = function (promises) {
  return new Promise(function (resolve, reject) {
    let result = [];
    let count = 0;
    for (let i = 0; i < promises.length; i++) {
      promises[i].then(function (data) {
        result[i] = data;
        if(++count == promises.length) { resolve(result); }},function (err) { reject(err); }); }}); }Promise.race = function (promiseAry) {
    return new Promise((resolve, reject) = > {
        for (let i = 0; i < promiseAry.length; i++) { promiseAry[i].then(resolve, reject); }}); };Promise.deferred = Promise.defer = function () {
  var defer = {};
  defer.promise = new Promise(function (resolve, reject) {
    defer.resolve = resolve;
    defer.reject = reject;
  })
  return defer;
}
/** * npm i -g promises-aplus-tests * promises-aplus-tests Promise.js */
try {
  module.exports = Promise
} catch (e) {
}

Copy the code

13. Implement a PorMISE scheduler with concurrency limits

// Implement the Promise scheduler with parallel limitations
// JS implements an asynchronous Scheduler with concurrency limits, which ensures that up to two tasks can run simultaneously. Refine the Scheduler class for the following code so that the following programs can output normally:

class Scheduler {
    constructor() {
        this.queue = [];
        this.curIndex = 0;
        this.maxIndex = 2;
    }
    add(promiseCreator) {
        this.queue.push(promiseCreator);
    }
    request(i) {
        if (!this.queue || !this.queue.length || this.runCounts >= this.maxCount) {
            return;
        }
        this.curIndex++;
        this.queue
            .shift()()
            .then(() = > {
                this.curIndex--;
                this.request();
            });
    }
    start() {
        for (let i = 0; i < this.maxIndex; i++) {
            this.request(i); }}}const timeout = (time) = >
    new Promise((resolve) = > {
        setTimeout(resolve, time);
    });

const scheduler = new Scheduler();

const addTask = (time, order) = > {
    scheduler.add(() = > timeout(time).then(() = > console.log(order)));
};

addTask(1000.'1');
addTask(500.'2');
addTask(300.'3');
addTask(400.'4');
scheduler.start();
// output: 2 3 1 4
Copy the code