1. Sort an array
  • Bubble sort
function popSort(list) {
    for (let i = 0; i < list.length - 1; i++) {
        for (let j = 0; j < list.length - i - 1; j++) {
            if (list[j] > list[j + 1]) {
                let temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
        }
    }
    return list;
}
Copy the code
  • Quick sort
function quickSort(list) {
    if (list.length <= 1) {
        return list;
    }
    let left = [];
    let right = [];
    let mid = Math.floor(list.length / 2);
    let midItem = list.splice(mid, 1);
    for (let i = 0; i < list.length; i++) {
        if (list[i] <= midItem) {
            left.push(list[i])
        } else {
            right.push(list[i]);
        }
    }
    return quickSort(left).concat(midItem, quickSort(right));
}
Copy the code
  • The sort order
function jsSort(list) {
    return list.sort((a, b) => a - b);
}
Copy the code
  1. Array to heavy
  • The new Set method
function uniq(list) {
    return [...new Set(list)];
}
Copy the code
  • Map method (no object because obj[1] cannot be distinguished from obj[‘1’])
function uniq(list) { let map = new Map(); let arr = []; for (let i = 0; i < list.length; i++) { if (! map.has(list[i])) { map.set(list[i], true); arr.push(list[i]); } } return arr; }Copy the code
  1. Write a function that returns an array of random integers between 2 and 32. The function can also pass in an argument that returns the length of the array.
function randomList(num = 10) {
    let initNum = 32 - 2 + 1;
    if (num > initNum) {
        num = initNum;
    }
    let list = Array(initNum).fill(1).map((item, index) => index + 2);
    let arr = [];
    for (let i = 0; i < num; i++) {
        let index = Math.floor((Math.random() * list.length));
        let temp = list.splice(index, 1)[0];
        arr.push(temp);
    }
    return arr;
}

Copy the code

4. Implement fn(1)(2)(3, 4) as 10

function curry(... args) { let _args = [...args]; function fn(... fnArgs) { _args = _args.concat(fnArgs); return fn; } fn.toString = function() { return _args.reduce((a, b) => a + b); } return fn; }Copy the code

5. Use promise to implement a function with a maximum of 10 requests

class Limit { constructor(max = 10) { this.max = max; this.count = 0; this.queue = []; } runCall(caller, ... args) { return new Promise((resolve, reject) => { let task = this.createTask(caller, resolve, reject, args); if (this.count >= this.max) { this.queue.push(task); } else { task(); }}); } createTask(caller, resolve, reject, args) { return () => { this.count++; caller(... args).then(resolve).catch(reject).finally(() => { if (this.queue.length > 0) { let task = this.queue.shift(); task(); }}); }; }}Copy the code

6. The frog can jump 1 or 2 steps at a time. Please experiment with a function to realize the total number of possibilities for jumping 10 steps

  • A classic Fibonacci function
function fb(n = 10) {
    if (n === 1) {
        return 1;
    }
    return n <= 2 ? 2 : fb(n - 2) + fb(n - 1);
}

Copy the code