JS basics day6

Posted by zypher11 on Tue, 14 Dec 2021 14:34:21 +0100

Review of knowledge points:

Function:

Formal and argument, unequal

Short circuit assignment

String splicing (string splicing is used when many values are returned)

Scope!!!!!!

Global, local and scope are relative (the child function can access the variables of the parent function)

Formal parameter: it is essentially a local variable

precompile

Promote var and named functions

// Code is not always executed from top to bottom

        console.log(111);


        // The event handling function is executed later, that is, only when clicked
        document.onclick = function () {  
            console.log(666);
        }

        console.log(222);



//Actual sequence
// 111
// 222
// 666
//Click the event to execute

Function: a data type

System function: prompt() confirm() eval()

Recursion: transfer and regression, call yourself

Example of Euclidean finding the greatest common divisor:

// Euclid seeks the greatest common divisor

        //  8 12 
        //  8 / 12   8
        //  12 / 8   4
        // 8 / 4     0
        // 4 / 0



        // 3 7 
        // 3 / 7  3
        // 7 / 3  1
        // 3 / 1  0
        // 1 / 0  

        // A% B = = = 0 B is the greatest common divisor

        function gcd(a , b) {  
            // if(a % b === 0) {
            //     return b
            // }
            // return gcd(b , a % b)

            return a % b === 0 ? b : gcd(b , a % b)
        }

        // When b is 0, a is their greatest common divisor
        // function gcd(a,b){
        //     return b===0?a:gcd(b,a%b);
        // }


        var res = gcd(28,18)
        console.log(res);

Arguments accepts argument problems

Arguments can be accepted as a pseudo array, and there are also corresponding arguments Length and subscript can be used when the argument cannot be determined

// In general, formal parameters are used to accept the value of arguments

        // You can use arguments to accept arguments

        function log() {  
            // Pseudo array / class array
            console.log(arguments);
            console.log(arguments[0]);
            console.log(arguments[1]);
            // console.log(arguments[2]);
            console.log(arguments.length);
        }
        log(1,2)

array

(store a set of data of the same data type)

An array is a group of numbers (generally speaking, the data types are the same), which is equivalent to defining many variables of the same data type at one time. If a single variable is a small container, an array is a large container with multiple grids (each grid is a small container).

Array is a data type.

// var a = 1 ;
        // var b = 3 ;
        // var c = 5 ;
        // var d = 2 ;
        // Array: stores a set of data of the same data type
        var arr = [1,3,5,,2] ;
        console.log(arr);

        // Array subscript method to access the value of the array: arr[index]
        console.log(arr[0]);

        // Length of array
        console.log(arr.length);

        console.log(arr[3]);

        // var arr = ['a' , 'b' , 'c'] ;


        // var arr = [1,2,'a','b',true] ;  //  Not very standard


var arr = [1,2,3] ;

        console.log(arr[10]);  // undefined

        // [1,2,3,,,,,,,20]
        // In fact, it lengthens the length of the array in disguise
        arr[10] = 20 ;

        console.log(arr[10]);

        console.log(arr.length);  // 11 

        console.log(arr);
 // var arr = [1,2,3] / / literal declaration

        // //An array is an object
        // var arr = new Array(1,2,3) ;  //  Instantiate an object array constructor (used to create objects)

        // console.log(arr.length);

        // console.log(arr);


        // var a = 1 ;    //  Literal declaration

        // var b = new Number(2) ;   //  Instantiate an object

        // console.log(a);
        // console.log(b);


        // var c = 'hello' ;
        // var d = new String('hello') ;


        // //When Array has only one parameter, it indicates the length of the Array
        // var arr2 = new Array(10) ;
        // console.log(arr2);



        // Declare an empty array and append values to the array
        var arr = [] ;
        arr[0] = 'a' ;
        arr[1] = 'b' ;
        arr[2] = 'c' ;
        arr[10] = 'f' ;

ergodic

Read each value in the array

Loop and for in are used to traverse the angle of the object

for in will automatically filter null values, which is generally not recommended.

for of traverses the value of the object

// Traversal: read every value in the array

        var arr = [1,2,3,4,,,,5] ;
        // console.log(arr[0]);
        // console.log(arr[1]);
        // console.log(arr[2]);


        // Loop traversal
        // for(var i = 0 ; i < arr.length ; i++) {
        //     console.log(arr[i]);
        // }

        // for in is used to traverse the corner mark of the object -- bug (automatically filter out null values)
        // for(var i in arr) {
        //     console.log(arr[i]);
        // }

        // for of traverses the value of the object
        for(var v of arr) {
            console.log(v);
        }

Case: generate a four digit random verification code

function rand(min , max){
    return Math.floor(Math.random()*(max - min)+min)
}

var arr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','1','2','3','4','5','6','A','B','C','D','E','F'];
    
        var res = '';
        for(i = 0 ; i < 4 ; i++){
            res += arr[rand(0 , arr.length)]
        }
        console.log(res)

Case: find the maximum value in the array

var arr = [10, 3 , 4 , 8 , 56 , 9 , 18];
        var max = arr[0];
        for(i= 0 ; i < arr.length; i++){
            if(max < arr[i]){
                max = arr[i]
            }
        }
        console.log(max);

Case: bubble sorting

Move a group of numbers one by one, and finally get a group of arrays from small to large

Loop twice. The inner loop is sorted after one-to-one comparison. However, since each number is ranked last, the value of the next number to be compared will be one less. Therefore, the inner loop can be optimized to subtract one cycle i after one less than the length of the loop array.

var arr = [12 , 4 , 6 , 45 , 9 , 8]
        for(var i = 0 ; i < arr.length - 1 ; i++){
            for(var j = 0 ; j < arr.length - 1 - i; j++){
                if(arr[j] > arr[j+1]){
                    var t = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = t
                }
            }
        }
        console.log(arr);

Cases: selecting sorting

Compare the size of each value in turn. By default, the first value is the minimum. If not, the minimum value will change, and the value of the lower corner will be exchanged with the minimum value. - reduce the number of value exchange by comparing the size in advance

var arr = [12 , 4 , 8 , 9 , 34 , 5];
        for(var i = 0 ; i < arr.length - 1 ; i++){
            var min = i;
            for(var j = i+1 ; j < arr.length ; j++){
                if(arr[min] > arr[j]){
                    min = j
                }
            }
            if(min !== i){
                var t = arr[i];
                arr[i] = arr[min];
                arr[min] = t;
            }
        }
        console.log(arr);

Basic method of array

push , append value to the tail

pop # deletes a value at the end of the array

unshift appends a value to the head of the array

shift deletes a value to the head of the array

        // push(v1,v2,v3...)    Appends a value to the end of the array
        // pop() deletes a value at the end of the array

        // unshift() appends a value to the head of the array
        // shift() deletes a value to the head of the array

        var arr = [1,2,3] ;
        // arr[3] = 'a' ;
        arr.push('a' , 'b' , 'c') ;
        console.log(arr);
        arr.pop() ;
        console.log(arr);

        arr.unshift('hello' , 'hi');
        console.log(arr);
        arr.shift();
        console.log(arr);



        var arr2 = [] ;
        for(var i = 0 ; i <= 10 ; i++) {
            // arr2[i] = i 
            arr2.push(i)
        }
        console.log(arr2);

push package

/ Append data to an array

        // var arr = [1,2,3,4] ;
        // arr[arr.length] = 'hello' ;
        // console.log(arr);


        // function push(n , n2 , n3) {  
        //     var arr = [1,2,3,4] ;
        //     arr[arr.length] = n ;
        //     arr[arr.length] = n2 ;
        //     arr[arr.length] = n3 ;
        //     console.log(arr);
        // }


        function push() {  
            var arr = [1,2,3,4] ;
            // Used to accept argument list --- pseudo array
            console.log(arguments);
            for(var i = 0 ; i < arguments.length ; i++) {
                console.log(arguments[i]);
                arr[arr.length] = arguments[i]
            }
            
            console.log(arr);
        }

        push('a','b','h' ,'t')

pop encapsulation

// Deletes the last value in the array

        function pop() {  
            var arr = [1,2,3,4,5] ;

            var arr2 = [] ;
            for(var i = 0 ; i < arr.length - 1 ; i++) {
                arr2.push(arr[i])
            }
            console.log(arr2);
        }

        pop()

Topics: Javascript ECMAScript