1. Locate the array element

function indexOf(arr, item) {
    return arr.indexOf(item);
}
Copy the code

2. Array summing

function sum(arr) {
    let res = 0;
    for(let i = 0; i < arr.length; i ++){
        res += arr[i];
    }
    return res;
}
Copy the code

forEach

function sum(arr) {
    let res = 0;
    arr.forEach((value, index, array) = > {
        res += value;
    })
    return res;
}
Copy the code

reduce

function sum(arr) {
    return arr.reduce((pre,cur) = >{
        returnpre + cur; })}Copy the code

3. Remove an element from an array to return a new array

filter

function remove(arr, item) {
    return arr.filter((res) = > {
        return res != item;
    })
}
Copy the code

for+push

function remove(arr, item) {
    let res = [];
    arr.forEach((value, index) = > {
        if(value != item) {
            res.push(value);
        }
    })
    return res;
}
Copy the code

4. Remove elements from the array to modify the original array

splice

function removeWithoutCopy(arr, item) {
    for(let i = 0; i < arr.length; i ++) {
        if(arr[i] === item){
            arr.splice(i,1); i --; }}return arr;
}
Copy the code

If I subtract one element from the array, I —

5. Add elements to return a new array

concat

function append(arr, item) {
    return arr.concat(item);
}
Copy the code

. extension

function append(arr, item) {
    return [...arr, item];
}
Copy the code

For \ slice shallow copy + push

6 Deletes the last element of the array

Returns a new array

function truncate(arr) {
    let newarr = arr.slice(0)
    newarr.pop()
    return newarr
}
Copy the code

7 Adding Elements

function prepend(arr, item) {
    let newarr = arr.slice(0);
    newarr.unshift(item);
    return newarr;
}
Copy the code

8 Delete the first element of the array

function curtail(arr) {
    let newarr = arr.slice(0);
    newarr.shift();
    return newarr;
}
Copy the code

9 Array Merging

function concat(arr1, arr2) {
    let newarr = arr1.concat(arr2);
    return newarr;
}
Copy the code
function concat(arr1, arr2) {
    let newarr = [...arr1, ...arr2];
    return newarr;
}
Copy the code

10 Add elements (add at the specified location)

function insert(arr, item, index) {
    let newarr = arr.slice(0);
    newarr.splice(index, 0, item);
    return newarr;
}
Copy the code

11 counts

function count(arr, item) {
    let num = 0;
    arr.forEach(val= > {
        if(val === item) num ++;
    })
    return num;
}
Copy the code

12 Find duplicate elements

function duplicates(arr) {
    let res = [];
    arr.forEach( v= > {
        if(arr.indexOf(v) ! == arr.lastIndexOf(v) && res.indexOf(v) === -1){ res.push(v); }})return res;
}
Copy the code

13 to the second power

function square(arr) {
    let res = arr.map((value, index, array) = > {
        return value * value;
    });
    return res;
}
Copy the code

14 Locate elements

function findAllOccurrences(arr, target) {
    let res = [];
    arr.forEach((value, index) = > {
        if(value === target) { res.push(index); }});return res;
}
Copy the code

15 Avoid global variables

function globals() {
    / / plus the let
    let myObject = {
      name : 'Jory'
    };

    return myObject;
}
Copy the code

16 Correct function definition

function functions(flag) {
    if (flag) {
      var getValue = function() { return 'a'; }}else {
      var getValue = function() { return 'b'; }}return getValue();
}
Copy the code

17 Use parseInt correctly

function parse2Int(num) {
    return parseInt(num, 10);
}
Copy the code

18 Be exactly equal

function identity(val1, val2) {
    return val1 === val2;
}
Copy the code

19 the timer

function count(start, end) {
    console.log(start++);
    var timer = setInterval(
        function(){
            if(start <= end) {
                console.log(start++);
            }else{
                clearInterval(timer); }},100);
    
    return {
        cancel: function(){
            clearInterval(timer); }}}Copy the code

20 Process Control

function fizzBuzz(num) {
    if(num % 3= = =0 && num % 5= = =0) {
        return 'fizzbuzz';
    }else if(num % 3= = =0) {
        return 'fizz';
    }else if(num % 5= = =0) {
        return 'buzz';
    }else if(num === null || typeofnum ! = ='number') {return false;
    }else{
        returnnum; }}Copy the code

21 Function parameters are passed

function argsAsArray(fn, arr) {
    return fn.apply(this, arr);
}
Copy the code

Function context

//apply
function speak(fn, obj) {
    return fn.apply(obj);
}
//call
function speak(fn, obj) {
    return fn.call(obj);
}
//bind
function speak(fn, obj) {
    return fn.bind(obj)();
}
Copy the code

23 Return function

function functionFunction(str) {
    return f = function(str2) {
        return str + ', '+ str2; }}Copy the code

24 Use closures

function makeClosures(arr, fn) {
    let result = [];
    ForEach and let can be circumvented
    arr.forEach((index,value) = > {
        result.push(() = > {
            returnfn(index); })})return result;
}
Copy the code

Execute function immediately

function makeClosures(arr, fn) {
    var result = [];
    ForEach and let can be circumvented
    for(var i = 0; i < arr.length; i ++) {
        (function (i) {
            result[i] = function() {
                return fn(arr[i])
            }
        })(i);
    }
    return result;
}
Copy the code

25 Quadratic encapsulation function

function partial(fn, str1, str2) {
    return function(str3) {
        returnfn(str1, str2, str3); }}Copy the code

26 USES the arguments

function useArguments() {
    let arr = Array.prototype.slice.call(arguments); // Convert to an array
    let res = 0;
    arr.forEach((value) = > {
        res += value;
    })
    return res;
}
Copy the code

27 Use apply to call the function

function callIt(fn) {
    return fn.apply(this, [].slice.call(arguments.1))}Copy the code

28 Quadratic encapsulation function

function partialUsingArguments(fn) {
    let args = [].slice.call(arguments.1);
    return reslut = function() {
        return fn.apply(this, args.concat([].slice.call(arguments))); }}Copy the code

29 It is difficult to make currie

function curryIt(fn) {
    let args = [];
    let result = function(arg) {
        args.push(arg);
        if(args.length < fn.length) {
            return result;
        }else{
            return fn.apply(this, args); }}return result;
}
Copy the code

30 or operation

function or(a, b) {
    return (a || b);
}
Copy the code

31 and operation

function and(a, b) {
    return (a && b);
}
Copy the code

32 module

function createModule(str1, str2) {
    let obj = {
        greeting: str1,
        name: str2,
        sayIt: function(){
            return obj.greeting + ', '+ obj.name; }}return obj;
}
Copy the code

33 Binary Conversion

function valueAtBit(num, bit) {
    let res = num.toString(2);
    return res[res.length - bit];
}
Copy the code

34 Binary Conversion

function base10(str) {
    return parseInt(str, 2);
}
Copy the code

35 Binary Conversion

function convertToBinary(num) {
    let res = num.toString(2);
    while(res.length < 8) {
        res = '0' + res;
    }
    return res;
}
Copy the code

36 multiplication

function multiply(a, b) {
    let str1 = a.toString();
    let str2 = b.toString();
    let lenA = (str1.indexOf('. ') = = = -1)?0: (str1.length - 1 - str1.indexOf('. '))
    let lenB = (str2.indexOf('. ') = = = -1)?0: (str2.length - 1 - str2.indexOf('. '))

                                                                                   
    let len = Math.max(lenA, lenB);
    let result = parseFloat(a * b).toFixed(len);
    return result;
    
}
Copy the code

37 Changing context

function alterContext(fn, obj) {
    return fn.apply(obj);
}
Copy the code

38 Change object attributes in batches

function alterObjects(constructor, greeting) {
    constructor.prototype.greeting = greeting;
}
Copy the code

39 Attribute traversal

function iterate(obj) {
    let result = [];
    for(let key in obj){
        if(obj.hasOwnProperty(key)){
            result.push(key+":"+obj[key]); }}return result;
}
Copy the code

40 Check whether digits are included

function containsNumber(str) {
    return /\d./.test(str);
}
Copy the code

41 Check for duplicate strings

function containsRepeatingLetter(str) {
    return /([a-zA-Z])\1/.test(str);
}
Copy the code

42 Check whether the letter ends with a vowel

function endsWithVowel(str) {
    return /[a,e,i,o,u]$/i.test(str);
}
Copy the code

43 Obtain the specified string

function captureThreeNumbers(str) {
    let arr = str.match(/\d{3}/);
    if(arr){
        return arr[0];
    }else{
        return false; }}Copy the code

44 Check whether the format is correct

function matchesPattern(str) {
    return /^\d{3}-\d{3}-\d{4}$/.test(str);
}
Copy the code

45 Check whether it conforms to the USD format

function isUSD(str) {
    return / ^ $\ \ d {1, 3} (\ d {3}) * (. \ \ d {2})? $/.test(str);
}
Copy the code