How to write several commonly used APIs

When programming, we will use many methods. In fact, we can also write these words by ourselves, encapsulate them in functions, and call them directly during use. It is also very convenient. Of course, what we write is not as good as the official website, but at least it is Meet your needs, let’s take a look at the writing of several common methods;
1. Decompose prime factors:
number is the number that needs to be decomposed, and the result of decomposition is returned.

function (number) {
    
    
         var arr = [];
         var numberCopy = number;
         while (number!=1) {
    
    
             for (var i = 2; i < =number; i++) {
    
    
                 if (number % i == 0) {
    
    
                     arr.push(i);
                     number = number / i;
                     break;
                 }
             }
         }
         return console.log(numberCopy + "=" + arr.join("*"));
     }

2. Determine whether it is a prime number

/**
 * 
 * @param {number} num  输入的某个数
 * @param   isPrime  判断是否为素数
 */

function isPrime(num){
    
    
    if(num<2){
    
    
      return false;
    }
    for(var j=2;j<num/2;j++){
    
    
        if(num%j==0){
    
     
           return false;
        }
    }
   return true;
}

3. Random string, often used to write verification codes

 
    function(len){
    
    
        var temp='';
        for(var i=65;i<65+26;i++){
    
    
              temp+=String.fromCharCode(i);
        }
        for(var i=97;i<97+26;i++){
    
    
            temp+=String.fromCharCode(i);
        }
        for(var i=48;i<48+10;i++){
    
    
            temp+=String.fromCharCode(i);
        }
        var res='';
        for(var j=0;j<len;j++){
    
    
              var index=this.getrandom(0,temp.length-1);
              res+=temp[index];
        }
      return console.log(res);
    },
    getrandom:function(max,min){
    
    
        return Math.floor(Math.random()*(max+1-min)+min);
    }

4. Find the character that appears the most in a string, print the character and the number of times it appears

    function(str){
    
    
        var recode={
    
    };
        var res={
    
    };
        for(var i=0;i<str.length;i++){
    
    
            if(recode[str[i]]){
    
    
                recode[str[i]]++;
            }else{
    
    
                recode[str[i]]=1;
            }
        }
         res.maxcount=0;
        for(var key in recode){
    
    
              if(recode[key]>res.maxcount){
    
    
                   res.member=key;
                   res.maxcount=recode[key];
              }
        }
          console.log(res);
    }

5. Remove duplicate items in the array
Method one:

     function (arr) {
    
    
       
          for (var i = 0; i < arr.length - 1; i++) {
    
    
             for (var j = 0; j < arr.length - i - 1; j++) {
    
    
                 if (arr[j] == arr[j + 1]) {
    
    
                      delete arr[j + 1];
                  }
              }
          }
          return console.log(arr);
          }
 

Method Two:

         function (arr) {
    
    
       
         var newarr=[];
         for(var i=0;i<arr.length-1;i++){
    
    
            if(newarr.indexOf(arr[i])==-1){
    
    
                    newarr.push(arr[i]);
             }
         } 
            return console.log(newarr);
         }

Method three:

   function (arr) {
    
    
       
         for(var i=0 , newarr=[] , hash={
    
    };i<arr.length;i++){
    
    
                if(hash[arr[i]]===undefined){
    
    
                    newarr.push(arr[i]);
                      hash[arr[i]]='123';
                }
        }
        return console.log(newarr);
        }

6. Sort the array
The items quoted here are some specific methods that are requirements, we can also add some other sorting methods ourselves, such as ascending order, descending order;

 function (arr, items) {
    
    
        if (!items) {
    
    
            items = function (a, b) {
    
    
                if (a > b) {
    
    
                    return1;
                } else if (a === b) {
    
    
                    return 0;
                } else {
    
    
                    return -1;
                }
            }
        }
        for (var i = 0; i < arr.length - 1; i++) {
    
    
            for (var j = 0; j < arr.length - i - 1; j++) {
    
    
                if (items(arr[j], arr[j + 1]) > 0) {
    
    
                    var temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return console.log(arr);
    }
// 升序方法
function items1(a, b) {
    
    
    if (a > b) {
    
    
        return true;
    }
}
// 降序方法
function items2(a, b) {
    
    
    if (b > a)
        return true;
}

7. Add data to the front of the array

  Array.prototype.unshift = function (arr) {
    
    
            for (var y = this.length + arguments.length - 1; y > 0; y--) {
    
    
                 this[y] = this[y - arguments.length];
             }
             for (var i = 0; i < arguments.length; i++) {
    
    
                 this[i] = arguments[i];
            }
             return this.length;
         }
         arr.unshift1(6, 7, 3, 0);
         console.log(arr);

8. Add after the array

  Array.prototype.push1 = function(arr){
    
    
             // this当前调用push1的数组
             // arguments 传递进来的所有参数

             for(var i = 0; i < arguments.length; i++){
    
    
                 this[this.length] = arguments[i];
             }

             return this.length;
         }
         arr.push1(10);
         console.log(arr);

9. Calculate the level of n, n can be input

      function (a) {
    
    
         var sum = 1;
         for (var i = 1; i <= a; i++) {
    
    
             sum *= i;
         }
         return console.log(sum);
     }

Guess you like

Origin blog.csdn.net/qq_44902858/article/details/110679045