Logical branch of JavaScript

catalogue

Logical branch

1, if conditional branch structure

1.if else syntax

2.if else exercise

2, switch conditional branch structure

1.switch syntax

2.switch exercise

3, Ternary operator (ternary conditional operator)

Logical branch

1, if conditional branch structure

1.if else syntax

Syntax 1: if() {}

if the condition is met, it will be executed; otherwise, it will not be executed

var year = 18;

if(year>=18){

console.log("Grown up");

}

 

Syntax 2: if(){}else {}

Whether the if condition is satisfied or not, the code in else will be executed

var year = 18;

if(year>=18){

console.log("Grown up");

}else{

console.log("under age");

}

Syntax 3: if(){}else if(){}else if() {}

Multiple else if judgments can be executed, but if none of the if conditions are met, none will be executed

var year = 18;
if(year>18){
console.log("Over 18 years old");
}else if(year=18){
console.log("Just over 18 years old");
}else if(year<18){
console.log("Less than 18 years old");
}

Syntax 4: if () {} else if () {} else{}

You can perform more else if judgment. Regardless of whether the if condition is satisfied or not, the final else code will be executed

var year = 18;
if(year>18){
console.log("Over 18 years old");
}else if(year=18){
console.log("Just over 18 years old");
}else if(year<18){
console.log("Less than 18 years old");
}else{
console.log("Cannot be equal to 0");
}

2.if else exercise

Guessing game:

Requirements: players and computers play guessing games

+ 1 = stone, + 2 = scissors, + 3 = cloth

+ what does the player determine by entering 1-3

+ what does the computer determine by random an integer between 1 and 3

Technical points:

+ get user input

+ randomly generate an integer

Technical point 1: get the content entered by the user

var player = prompt('Please enter a 1-3 Integer between:1=stone,2=scissors,3=cloth');
console.log(player)
prompt You can pop up an input box
 Then the user input content is returned in the form of string
 I can receive this data with a variable

Technical point 2: randomly generate an integer

js method: Math.random()
You can return a 0-1 Random decimal between
 And the random decimal may be 0, but it must not be 1
var computer = parseInt(Math.random()*3)+1;
console.log(computer);

Completion requirements

1. The player punches: the player determines what to do by entering 1-3

var player = prompt('Please enter an integer between 1 and 3: 1 = stone, 2 = scissors, 3 = cloth ');

2. Computer punch: randomly generate an integer between 1 and 3

var computer = parseInt(Math.random()*3)+1;

3. Judgment results

if(palyer==1){
//Player out of stone
          if(computer==1){
              console.log("Computer makes stone");
              console.log("Player out of stone");
              console.log("it ends in a draw");
          }else if(computer==2){
            console.log("Computer scissors");
              console.log("Player out of stone");
            console.log("Player wins");
          }else if(computer==3){
            console.log("Computer cloth");
            console.log("Player out of stone");
            console.log("Computer wins");
          }
      }else if(palyer==2){//Player out scissors
          if(computer==1){
              console.log("Computer makes stone");
              console.log("Player out scissors");
              console.log("Computer wins");
          }else if(computer==2){
            console.log("Computer scissors");
              console.log("Player out scissors");
            console.log("it ends in a draw");
          }else if(computer==3){
            console.log("Computer cloth");
            console.log("Player out scissors");
            console.log("Player wins");
          }
        }else if(palyer==3){//Player out cloth
          if(computer==1){
              console.log("Computer makes stone");
              console.log("Player out cloth");
              console.log("Player wins");
          }else if(computer==2){
            console.log("Computer scissors");
              console.log("Player out cloth");
            console.log("Computer wins");
          }else if(computer==3){
            console.log("Computer cloth");
            console.log("Player out cloth");
            console.log("it ends in a draw");
          }
      }

2, switch conditional branch structure

1.switch syntax

               /*Syntax:
                    switch((variable){
                        case Value 1:
                            If the value of the variable = = = value 1, execute the code here;
                            break;
                        case Value 2:
                            If the value of the variable = = = value 2, execute the code here;
                            break;
                        case Value 3:
                            If the value of the variable = = = value 3, execute the code here;
                            break;
                        ....
                        default:
                            If all values are not equal, execute the code here;
                            break;
                    }*/

Penetration of switch:

Switch judges the conditions. If the conditions are met, the conditions in the case will be executed. If there is a break, the following code will not be executed. If there is no break, the following code will continue to be executed without conditional judgment. This is called switch penetration

2.switch exercise

According to a six digit date, judge the day of the year. (written using switch penetration)

Requirements:

+You need to store the year, month and day separately

+You need to create a variable to receive the number of days of the year

+You need to judge the number of days in each month and the number of days in February of leap year

Completion requirements

var date = Number(prompt("Please enter an 8-digit date"));
var year = parseInt(date / 10000);//Take the first four digits of the integer as the year
var yue = parseInt(date / 100) % 100;//First take the whole number of days, and then take the rest as the month
var day = parseInt(data % 100);//The last two digits are days
var days = 0;//Number of days used to receive the whole year
var rn = 0;//leap year

switch (yue) {
            case 12:
                days += 30;
            case 11:
                days += 31;
            case 10:
                days += 31;
            case 9:
                days += 30;
            case 8:
                days += 31;
            case 7:
                days += 30;
            case 6:
                days += 31;
            case 5:
                days += 30;
            case 4:
                days += 31;
            case 3:
                if (year% 4 == 0 && year% 100 != 0 || year% 400 == 0) {
                    rn = 28;
                } else {
                    rn = 29;
                }
                days += rn;
            case 2:
                days += day + 31;
                console.log(days);
                break;
            default:
                days = day ;
                console.log(days);
        }
        alert(yue+"month"+day+"Day is"+year+"The second day of the year"+days+"day");

3, Ternary operator (ternary conditional operator)

Ternary operator (ternary conditional operator): expression? Value 1: value 2
If the value of the expression is true, the result takes 1. Otherwise, the value is 2

==This operator consists of three operands and two symbols

= = syntax: condition? If the condition is true, execute this time: if the condition is false, execute here

==Similar to if # else

// If you are older than 18 years old, you are an adult, otherwise you are a minor
var age = 23;
var info = age>=18?'adult':'juveniles'
console.log(info);

 

Keywords: Javascript

Added by kee1108 on Tue, 08 Feb 2022 12:52:54 +0200