September 16, 2021 --- basic JavaScript syntax: branch structure

1. Process control of procedures

2. Three structures of program control

  1. Sequential structure: execute each statement from top to bottom and from left to right in sequence. It is not allowed to skip any statement.
  2. Select structure: execute a piece of code according to conditions.
  3. Cycle conditions; If a certain condition is met, a certain piece of code is executed repeatedly.

3. What are the statements that implement the selection structure

  1. ? :
  2. if
  3. switch

Ternary operator?:

if

Single branch selection

if(condition){
	Statement group;
}

<script>
        //As long as the data is obtained from the outside, it is of type String.
        //i. Whether to issue the notice of Tsinghua University according to the results? seven hundred and twenty
        var i_score = prompt('Please enter your grade:');
        if(!isNaN(i_score)){  //First judge whether the input is a number, either isNaN or a number
            if(i_score > 600){
                        alert('congratulations!')
                    }
        }
        
        /*
Input any two numbers, and then exchange position output (for example, a=4,b=5, output a=5,b=4)

        */
        var a = prompt("Please enter a number");
        var b = prompt('Please enter another number');
        console.log("Before exchange\n"+ 'a' + '=' + a +'\nb' + '=' + b);

        var t ;
        t = a;
        a = b;
        b = t;
        console.log("After exchange\n"+ 'a' + '=' + a +'\nb' + '=' + b);


        /*
Enter any three numbers and output from large to small
        */
        var a = prompt('please');
        var b = prompt('please');
        var c = prompt('please');
        var t;
        //Idea: let a store the maximum of the three numbers. First judge the size of a and B. If a is less than B, exchange. At this time, a stores the larger value between a and B, and then judge the size of a and C. If a is less than C, exchange. At this time, what is stored in a is the maximum value of a, B and C; Then judge the size of B and C. if B is less than C, exchange. At this time, the larger value between B and C is stored in B, that is, the second largest value, and the smallest value among the three is stored in C.
        //Always keep the maximum value stored in A
        //Judge the size of a and B. if B is large, exchange
        if(a<b){
            t = a;
            a = b;
            b = t;
        }
        //Judge the size of a and C. if C is large, exchange
        if(a < c){
            t = a;
            a = c;
            c = t;
        }
        //Always keep the second largest value in B.
        //Judge the size of B and C. if C is large, exchange
        if(b < c){
            t = b;
            b = c;
            c = t;
        }
        console.log(a,b,c);
    </script>

Double branch selection

if(condition){
	Statement group;
}else{
	Statement group;
}

Multi branch selection

if(condition){
	Statement group;
}else if(condition){
	Statement group;
}...

...else if((condition){
	Statement group;
}else{
	Statement group;
}

<script>
        /*
        Enter any number to judge the day of the week?
        */
        // var a = prompt('Please enter ');
        // if(a == 1){
        //     alert('monday ');
        // }else if(a == 2){
        //     alert('tuesday ');
        // }else if(a == 3){
        //     alert('wednesday ');
        // }else if(a == 4){
        //     alert('thursday ');
        // }else if(a == 5){
        //     alert('friday ');
        // }else if(a == 6){
        //     alert('saturday ');
        // }else{
        //     alert('sunday ');
        // }
        // //Judge that the score is excellent (90-100), good (80-89), medium (70-79), poor (60-69) and fail (< 60)
        // var score = prompt('Please enter ');
        // if(score > 0 && score <=100){
        //     if (score >= 90 && score <= 100){
        //     alert('you');
        //     }else if(score >= 80){
        //         alert('liang');
        //     }else if(score >= 70){
        //         alert('zhong');
        //     }else if(score >= 60){
        //         alert('cha');
        //     }else{
        //         alert('bujige');
        //     }
        // }else{
        //     alert('lihai');
        // }
        //Design a simple calculator with +, -, *, /,%
        var a = Number( prompt('First number'));
        var ch = prompt('operator');
        var b = Number(prompt('Second number')) ;
        if(ch == '+'){
            alert(a + '+' + b + '=' + (a + b));
        }else if(ch == '-'){
            alert(a + '-' + b + '=' +(a - b));
        }else if(ch == '*'){
            alert(a + '*' + b + '=' +(a * b));
        }else if(ch == '/'){
            alert(b !== 0 ?  a + '/' + b + '=' +(a / b) : 'Divisor cannot be 0');
        }else{
            alert(b !== 0 ? a + '%' + b + '=' +(a % b) : 'Divisor cannot be 0');
        }
    </script>
</body>

Switch switch statement

switch(expression){
	case expression : Statement group;[break;]
	case expression : Statement group;[break;]
	......
	case expression : Statement group;[break;]
	[default : Statement group;]
}


Rule: first calculate the value of the expression after switch. If the value is consistent with the value of the expression after a case, execute the statement group after the case. If there is a break, exit the switch statement; If there is no break. Continue to execute all subsequent statement groups until the end of break or closing brace is encountered.

<script>
        // Enter any number to judge the day of the week?
        // var z = parseInt(prompt('a number ');
        // switch(z){
        //     Case 1: Alert ('monday '); break;
        //     Case 2: Alert ('tuesday '); break;
        //     Case 3: Alert ('wednesday '); break;
        //     Case 4: Alert ('thursday '); break;
        //     Case 5: Alert ('friday '); break;
        //     Case 6: Alert ('saturday '); break;
        //     Case 7: Alert ('sunday '); break;
        //     Default: Alert ('Invalid ');
        // }

        /*
Enter an integer of 0-6 to judge which day is a working day and which day is a rest day?
        */
        // Var a = parseInt (prompt ('integer of 0-6 ');
        // switch( a ){
        //     Case 1: Alert ('working day '); break;
        //     Case 2: Alert ('working day '); break;
        //     Case 3: Alert ('working day '); break;
        //     Case 4: Alert ('working day '); break;
        //     Case 5: Alert ('working day '); break;
        //     Case 6: Alert ('rest day '); break;
        //     Case 0: Alert ('rest day '); break;
        //     Default: Alert ('illegal ');
        // }

    //Calculate what day of the year is a day?
    // 2020 8 1
    // July is the time to add a few months according to the month
        var year = parseInt(prompt('particular year'));
        var month = parseInt(prompt('month'));
        var day = parseInt(prompt('date'));
        var sum = 0;
        switch( month ){
            case 12 : sum += 30;
            case 11 : sum += 31;
            case 10 : sum += 31;
            case 9 : sum += 30;
            case 8 : sum += 31;
            case 7 : sum += 31;
            case 6 : sum += 30;
            case 5 : sum += 31;
            case 4 : sum += 30;
            case 3 : sum += 31;
            case 2 : !(year % 4) && year %100 || !(year % 400) ? sum += 29  : sum+=28;
            case 1 : sum += day;
            // Default: Alert ('Invalid ');            
        }
        alert(sum);

        
    </script>

Calculate what day of the year is a day?
This case makes good use of the penetration of switch. First of all, we should be able to judge. We judge according to the month. It takes a total of several months to add. Knowing this, we can know that the expression behind switch is month; Then, you must start from case 12 and write it backwards. In this way, if the entered date is November 28, then the case matches to month 11, skip case 12. Starting from case 11, execute each subsequent statement in turn, and add the days of October, September,..., February and January in turn, so as to get the total time; Finally, it should be noted that if the user enters December, the number of days in the first 11 months will be added, and then the number of days in 12 will be added. If the user enters September, the number of days in the first 8 months will be added, and then the number of days in September will be added; If the user enters January, it is good to directly add the number of days in January.

application

 <script>
	// switch
        // 1) Enter any number to judge the day of the week? 
        //  var z = parseInt(prompt('a number ');
        // switch(z){
        //     Case 1: Alert ('monday '); break;
        //     Case 2: Alert ('tuesday '); break;
        //     Case 3: Alert ('wednesday '); break;
        //     Case 4: Alert ('thursday '); break;
        //     Case 5: Alert ('friday '); break;
        //     Case 6: Alert ('saturday '); break;
        //     Case 7: Alert ('sunday '); break;
        //     Default: Alert ('Invalid ');
        // }

        // 2) Enter an integer of 0-6 to judge which day is a working day and which day is a rest day? 
        // Var a = parseInt (prompt ('integer of 0-6 ');
        // switch( a ){
        //     Case 1: Alert ('working day '); break;
        //     Case 2: Alert ('working day '); break;
        //     Case 3: Alert ('working day '); break;
        //     Case 4: Alert ('working day '); break;
        //     Case 5: Alert ('working day '); break;
        //     Case 6: Alert ('rest day '); break;
        //     Case 0: Alert ('rest day '); break;
        //     Default: Alert ('illegal ');
        // }

        // 3) Enter a month and output the number of days in this month?
        // var year = parseInt(prompt('year '); 
        // var month = parseInt(prompt('month ');
        // switch( month ){
        //     case 1 :
        //     case 3 : 
        //     case 5 :
        //     case 7 :
        //     case 8 :
        //     case 10 :
        //     Case 12: Alert ('31 days'); break;
        //     case 4 :
        //     case 6 :
        //     case 9 :
        //     Case 11: Alert ('30 days'); break;
        //     case 2 : ! (year % 4) && (year % 100) || ! (year % 400) ?  Alert ('29 days'): alert('28 days');
        // }

        // 4) Judge excellent, medium poor, fail 100-90 89 - 80 79-70 69-60 < 60

        //Method 1
        // var i_score = Number(prompt('your score ');
        // if(i_score >= 0 && i_score <= 100){
        //     switch (true){
        //         case i_ score>=90 && i_ Score < = 100: Alert ('excellent '); break;
        //         case i_ Score > = 80: Alert ('good '); break;
        //         case i_ Score > = 70: Alert ('medium '); break;
        //         case i_ Score > = 60: Alert ('poor '); break;
        //         case i_ Score < 60: Alert ('fail ');
        //     }  
        // }else{
        //     alert('Please enter a number from 0 to 100 ');
        // }
        //Method 2
        // var i_score = parseInt(prompt('your score ') / 10); 
        // switch(i_score){
        //     case 10 :
        //     Case 9: Alert ('excellent '); break;
        //     Case 8: Alert ('good '); break;
        //     Case 7: Alert ('medium '); break;
        //     Case 6: Alert ('poor '); break;
        //     Default: Alert ('failed ');
        // }


        // 5) Calculate what day of the year is a day? 
        // var year = parseInt(prompt('year ');
        // var month = parseInt(prompt('month ');
        // var day = parseInt(prompt('date ');
        // var sum = 0;
        // switch( month ){
        //     case 12 : sum += 30;
        //     case 11 : sum += 31;
        //     case 10 : sum += 31;
        //     case 9 : sum += 30;
        //     case 8 : sum += 31;
        //     case 7 : sum += 31;
        //     case 6 : sum += 30;
        //     case 5 : sum += 31;
        //     case 4 : sum += 30;
        //     case 3 : sum += 31;
        //     case 2 : !(year % 4) && year %100 || !(year % 400) ? sum += 29  : sum+=28;
        //     case 1 : sum += day;
        //     //Default: Alert ('Invalid ');            
        // }
        // alert(sum);

        //This case makes good use of the penetration of switch. First of all, we should be able to judge. We judge according to the month. It takes a total of several months to add. Knowing this, we can know that the expression behind switch is month; Then, you must start from case 12 and write it backwards. In this way, if the entered date is November 28, then the case matches to month 11, skip case 12. Starting from case 11, execute each subsequent statement in turn, and add the days of October, September,..., February and January in turn, so as to get the total time; Finally, it should be noted that if the user enters December, the number of days in the first 11 months will be added, and then the number of days in 12 will be added. If the user enters September, the number of days in the first 8 months will be added, and then the number of days in September will be added; If the user enters January, it is good to directly add the number of days in January.

        // //6) implement a simple calculator with switch 
        //     var i_one = Number(prompt('first number ');
        //     var ch = prompt('operator ');
        //     var i_two = Number(prompt('second number ');
        //     switch( ch ){
        //         case '+' : alert(i_one + '+' + i_two + '=' + (i_one+i_two));break;
        //         case '-' : alert(i_one + '-' + i_two + '=' + (i_one-i_two));break;
        //         case '*' : alert(i_one + '*' + i_two + '=' + (i_one*i_two));break;
        //         case '/' : i_two === 0 ? alert('divisor cannot be 0 '): Alert (i_one +' / '+ i_two +' = '+ (i_one / i_two)); break;
        //         case '%' : i_two === 0 ? alert('divisor cannot be 0 '): Alert (i_one +'% '+ i_two +' = '+ (i_one% i_two)); break;
        //     }

        // 7) Test personality according to blood type and gender 
            // var blood = prompt('blood group A B C 0 ');
            // var sex = prompt('gender M W ');
            // switch (sex ){
            //     case 'M' : switch (blood){
            //         Case 'a': Alert ('male A');break;
            //         Case 'B': Alert ('male B');break;
            //         Case 'ab': Alert ('male AB');break;
            //         Case 'O': Alert ('male O ');
            //     };
            //     case 'W' : 
            //     switch (blood){
            //         Case 'a': Alert ('female A');break;
            //         Case 'B': Alert ('female B');break;
            //         Case 'ab': Alert ('female AB');break;
            //         Case 'O': Alert ('female O ');
            //     }
            // }
        // 8) Design a simple guessing game
            var play_one = parseInt(prompt('Please enter 0 for stone, 1 for scissors and 2 for cloth')) ;
            var play_two = parseInt(prompt('Please enter 0 for stone, 1 for scissors and 2 for cloth')) ;
            switch(play_one){
                case 0 : 
                    switch(play_two){
                        case 0 : alert('it ends in a draw');break;
                        case 1 : alert('Win first');break;
                        case 2 : alert('Backhand win');
                    }break;
                case 1 : 
                    switch(play_two){
                        case 0 : alert('Backhand win');break;
                        case 1 : alert('it ends in a draw');break;
                        case 2 : alert('Win first');
                    }break;
                case 2 : 
                    switch(play_two){
                        case 0 : alert('Win first');break;
                        case 1 : alert('Backhand win');break;
                        case 2 : alert('it ends in a draw');
                    }
            }

    </script>

Tags: Javascript

Posted on Sat, 18 Sep 2021 15:24:19 -0400 by phenley