Front end learning notes: js the second day -- operators, process control statements, IF, loops, functions, scope of variables, DOM element control, lucky draw cases

Operator:

  • Assignment operator = + = - = * =/=
  • Arithmetic operator + - * / divide by% (remainder) the remainder is positive or negative, and the positive or negative is determined by the divisor
  • Relational operator = = = ======   !== > = < = > < < get Boolean types
    • = = as like as two peas, the same value is not considered.
  • Logical operators & & and | or! Reverse
    • &&As long as one is false, the result is false | as long as one is true, the result is true
 <script>
           
            // Assign the value of a to a
            var a=2;
            // a=a+2; Abbreviated as a+=2;
            // a+=2;
            // a=a-2, abbreviated as a-=2;
            // a=a/2, abbreviated as a/=2;
            // a=a*2, abbreviated as a*=2;
           
            console.log(10/3);
            console.log(10%-3);

            console.log(-a);
          
            console.log(1===1);
            console.log(1!='1');
            a=800;
            // Cast when the code performs Boolean and numeric type comparison step by step
            // console.log(10<a<100)
          
            // In the case of pure numerical and or non. The result is also a numerical value.
            // console.log(a>10&&a<100);
            // The code executes from left to right
            console.log(2&&3&&4&&0&&3)
            console.log(0||0||4||4||3)
        </script>

Process control statement IF

Format I

if (conditional){

When the condition is true, execute the code.

}

Format II

if (conditional){

When the condition is true, execute the code.

}else{

When the condition is false, execute the code.

}

<script>
            var a=prompt('Please enter salary');
            a=parseInt(a);
          
             //if(age<18){
                 // Popup 
               // alert('older ')
              //  }

             if(age<18){
                 alert('Not old enough')
             }else{
                 alert('Internet cafe')
             }


            //0 is false undefined is false 
            // All strings are true
            // if(!1){
            //     console.log('hahah')
            // }

</script>

Multi condition judgment: (if statement has jump)

If (condition 1){

When condition 1 is true, execute the statement and jump out directly

} else} if (condition 2){

When condition 2 is true, execute

} else} if (condition 3){

When condition 3 is true, execute

}.....

   else{

If the above conditions are not met, it shall be implemented

          }

 <script>

if(a<=36000){
                // For different variable types, if splicing is required, use+
                alert('You need to pay tax'+a*0.03);
            }else if(a<=144000){
                var sum=36000*0.03+(a-36000)*0.1;
                alert('You need to pay tax'+sum)
            }else if(a<=300000){
                var sum=36000*0.03+(144000-36000)*0.1+(a-144000)*0.2;
                alert('You need to pay tax'+sum)
            }else if(a<=420000){
                var sum=36000*0.03+(144000-36000)*0.1+(300000-144000)*0.2+(a-144000)*0.2;
                alert('You need to pay tax'+sum)
            }
        </script>

loop

Loop: the code used to implement traversal is sequential

  • Step 1: execute a, initialization
  • Step 2: judge whether it complies with b. if it does, execute step 3, otherwise it will jump out of the loop
  • Step 3: execute the loop body and execute c. Repeat step 2
 <script>
        //for(a initialization; b condition; c condition / times of continuation){
                // Circulatory body
            // }


        for(var a=1;a<=100;a++){
               console.log(a);
          }
    
</script>

function

Function: encapsulation, which encapsulates repeated code. Generally, these codes have special purposes

Function definition: the naming rules for function names are the same as those for variable names

  <script>
    // Function function name (){
            //     Function body
            // }
            // Function name ()

            // Define function
            function fn(){
                console.log('zsh')
                console.log('sh')
            }

            // Using a function is equivalent to executing the code of the function body
            fn()
          
        </script>
        

Parametric function:

When defining a function, the parameters are called formal parameters, which is equivalent to declaring variables

When using a function, the parameters are called arguments, which is equivalent to assigning values to variables

 <script>
            
            // Parameters when defining a function, called formal parameters, are equivalent to declaring variables
            function  fn(a){
                console.log(a)
            }
          
            fn(2)
            fn(3)
        </script>

Function with return value:

Use this function to get the contents after return

<script>
    
            function fn(a,b){
                // Use this function to get the contents after return
                return a+b;
            }
            var c=fn(1,2)
            document.write(c)

            function mul(a,c){
                return a*c
            }
            mul(12,2)
        </script>

Scope of variable

The variables declared in js only have the scope of the function.

Scope of the declared variable. The scope of action of the {variable of the function nearest to the variable is}.

<script>

        function fn(){  
          var a=10;
          console.log(a)
           }

        fn()
        var b;
       console.log(b);
        //No error will be reported and undefined will be printed

</script>

Variable will promote the declaration to the front of the whole scope, and the assignment is still in the original position

<script>
        console.log(a);
         var a=100;

         // Equivalent to
        // var a;
        // console.log(a)
        // a=100;
        

</script>

Global variables and local variables exist at the same time, and local variables take effect

<script>
         var a=1;
        function fn(){
             console.log(a);
            var a='hello';
            // var a;
            console.log(a);
            // a='hello'
         }
         fn();

</script>

For undeclared variables, the scope takes effect after assignment (not recommended)

         <script>
                  a=100;
                 console.log(a);
                 console.log(b);
                 b=100;
        </script>

Control of DOM elements

Array: an array stores a set of data

Get an element of the array: the array name [subscript] subscript starts from 0

Get the length of the array: array name length;

Get the tag by ID Name: document Getelementbyid ('id name ');

Event: get tag on + event type (click)=function() {executed code};

Modify content: get label innerHTML = 'modified content'

Generate random number: math Random() generates random numbers between 0 and 1, including 0 and excluding 1

Timer: setInterval(function() {code executed every other period of time}, time ms)

Stop timer: clearinterval (timer name)

 <body>
        <button id="btn">Button</button>
       
        <script>
            
            var list=['a','b','c','d'];
            
            console.log(list[2])
            // Array length array name length
            console.log(list.length)
            
            var tag=document.getElementById('btn');
          
            tag.onclick=function(){
               
                tag.innerHTML='It's too late';
                
                console.log(Math.random())
                
                // Timers can be superimposed
                setInterval(function(){
                    console.log(Math.random())
                   
                },1000)
            }
        </script>
    </body>

Lucky draw cases:

<body>
        <!-- id The name is similar to our ID number. It is unique. -->
        <button id="start">start</button>
        <button id="end">end</button>
        <h4 id="txt">Let's start the lottery</h4>

        <script>
            var start=document.getElementById('start');
            var end=document.getElementById('end');
            var txt=document.getElementById('txt');
            var list=['ddd','xxx','vvvv','aaa','bbb','CCC','XXX'];
            var timer;
            // Click start
            start.onclick=function(){
               clearInterval(timer)
               timer= setInterval(function(){
                    // The random number of subscripts is generated
                    var n=parseInt(Math.random()*list.length);
                    console.log(n);
                    // Modify html content
                    txt.innerHTML=list[n];
                },50)
            }
            // Click end
            end.onclick=function(){
               
                // Stop timer clearinterval (timer name)
                clearInterval(timer);
            }
            
        </script>
    </body>

Added by phpstuck on Fri, 14 Jan 2022 00:54:24 +0200