JS title

Bubble Sort

bubbleSort function (ARR) {
      // the outer loop round number r from 0 to arr.length
        for (var r = 0; r <arr.length; r ++) {
         // inner loop through each control element i from 0 to-R & lt arr.length
         for (var i = 0; i <arr.length-R & lt; i ++) {
           // if the value at location i arr> value of position i + 1
           IF (arr [i ]> arr [i + 1]) {
            // value of arr exchange position i and i + 1 position
            arr [i] = ^ arr [i + 1];
            arr [i + 1] = ^ arr [i];
            ARR [I] = ^ ARR [I +. 1];
           }
          }
         }
        }
        var ARR = [2,5,7,3,6,4,1];
        the console.log (String (ARR));
        bubbleSort (ARR) ;
        console.log (String (arr));

arr.sort()

Custom comparator function unit

CMP function (A, B) {
         return ab &;
        }
        // comparator functions as objects pass sort process
        arr.sort (cmp); // write directly cmp, not with ();
 the console.log (ARR);

of arr1 = var [9,8,7,6,5,4,3,2,1,0];
         arr1.sort (function (A, B) {return ab &}); // anonymous function
        console.log (arr1 );

 

Stack

first in last out, last out
        var ARR = [];
        for (var. 1 = I; I <=. 5;
         I ++) {
        //arr.push adding new elements to the end of the array
           arr.push ( "passenger" + i );
           the console.log (String (ARR));
       }
       the while (arr.length> 0) {
        //arr.pop remove and return the last element of the array
         var = arr.pop last ();
         the console.log (last + " off ");
         the console.log (String (ARR));
        }

    // arr.shift (delete the beginning of the first element of the array), and returns the first element
    // arr.unshift () added to the beginning of an array of one or more element, and returns the new length
        var arr2 = [] ;
        for (var. 1 = I; I <=. 4; I ++) {
         arr2.unshift ( "passenger" + I);
         the console.log (String (arr2 is));
        }
        while(arr2.length>0){
         var first = arr2.shift();
         console.log(first+"下车");
        }
 
队列  first in firs out
    var phone = 5;
   var queue = [];
   for(var i = 1 ; i<=5;i++){
    queue.push("顾客:"+i);
   }
   console.log(String(queue));
   while(phone>0){
    var first = queue.shift();
    phone--;
    console.log(first+"购买成功");
    console.log(String(queue));
   }

 

Random codes

<button onclick="GetCode(6)">验证码</button>

<script>

 the init function () {
                      var char = [];
                for (var = 97 U; U <= 122; U ++) {
                        // converted to unicode character code as a character i stored in the char
                        char.push (String.fromCharCode ( U));
                }
                for(var u = 65;u<=90;u++){
                char.push(String.fromCharCode(u));
                }
                for (n-var = 0; n-<=. 9; n-++) {
                        char.push (n-);
                }  
                return char;
                }
             
                       
                function getCode (COUNT) {
                var char = the init ();
                for (var I = 0, code = [ ], the hash = []; I <COUNT; I ++) {
                        // Unique random number
                        // generates a random number r ~ 61 is between 0
                        do {
                        var r = the parseInt (Math.random () * 62 is);
                        } while (hash [char [r] ] = undefined!);
                random r // get is not repeated in the superscript character char
                        hash [char [r]] = true;
                        // char character position of r is added to the code array
                        code.push (char [r]);
                }
               
                 the console.log (code.join ( ""));
                   return code.join ( "");
                }
</script>

 

 

 

 

 

 

 

 

 

 

    

 

Guess you like

Origin www.cnblogs.com/zhanghaifeng123/p/11300572.html