JavaScript process control - Branch

catalogue

if branch statement

if else double branch statement

Judge leap year cases

if else if multi branch statement

Ternary expression

Case of digital complement 0

switch multi branch statement

Judge week case

★★ display time case ★★

There are three main structures of process control, namely sequence structure, branch structure and cycle structure.

Sequential structure: the program is executed in the order of the code.

Branching structure

if branch statement

Syntax structure:

if (Conditional expression) {

    sentence;

}

Execution idea: if the conditional expression is true, execute the statement in braces; If the conditional expression is false, the statement inside the braces will not be executed, and the statement after the braces will be executed.

if else double branch statement

Syntax structure:

if (Conditional expression) {

    Statement 1;

} else {

    Statement 2;

}

Execution idea: if the conditional expression is true, execute statement 1; otherwise, execute statement 2.

Judge leap year cases

<script>
// Algorithm: a leap year is one that can be divided by 4 but cannot be divided by 100, or a leap year is one that can be divided by 400 (one leap in four years, no leap in one hundred years, and then leap in four hundred years)
// Pop up the prompt input box, let the user enter the year, take this value and save it in the variable
// Use the if else statement to judge whether it is a leap year. If it is a leap year, execute the output statement in if braces, otherwise execute the output statement in else
// Pay attention to the writing of and & & or 𞓜 inside, and pay attention to the way to judge the division by taking the remainder as 0

        var year = prompt('Please enter a year:')
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            alert('You entered a leap year')
        } else {
            alert('You entered a normal year')
        }
</script>

if else if multi branch statement

Syntax structure:

if (Conditional expression 1) {
    Statement 1;
} else if (Conditional expression 2) {
    Statement 2;
} else if (Conditional expression 3) {
    Statement 3;
} else {
    Last statement;
}
Note: the last one is not if

Implementation idea:

If the conditional expression 1 is satisfied, execute statement 1. After execution, exit the entire if branch statement;

If the condition expression 1 is not satisfied, judge the condition expression 2. If it is satisfied, execute statement 2, and so on;

If none of the above conditional expressions holds, execute the statements in else.

Ternary expression

Syntax structure:

Conditional expression ? Expression 1 : Expression 2

Execution idea: if the value of the conditional expression is true, return the value of expression 1; if false, return the value of expression 2.

Case of digital complement 0

<script>
    // The user enters a number between 0 and 59
    // If the number is less than 10, fill 0 in front of this number (add 0 to splice), otherwise do not operate
    // Accept the return value with a variable and output
    var time = prompt('Please enter a 0 ~ 59 A number between');
    // Ternary expression? Expression 1: expression 2 
    var result = time < 10 ? '0' + time : time;   //Assign the return value to a variable
    alert(result);
</script>

switch multi branch statement

Syntax structure:

switch (expression) {
    case value1:
        Execute statement 1;
        break;
    case value2:
        Execute statement 2;
        break;
    ...
    default:
        Execute the last statement;
}

Execution idea: use the value of the expression to match the option value behind the case. If it matches, execute the statement in the case. If it doesn't match, execute the statement in default.

matters needing attention:

  1. Expressions in development are often written as variables
  2. When the value of the expression matches the value in the case, it is {congruent
  3. If there is no break in the current case, the switch will not exit and the next case will continue to be executed

Judge week case

Suppose the 1st of this month is Wednesday. Let the user enter the date of the current month (i.e. 1-31 days). If it is not 1-31 days, you will be prompted to re-enter. If the input is correct, the day of the week will be returned.

    <script>
        var date = prompt('Please enter 1-31 Any date of:')
        if (date < 1 || date > 31) {
            alert('Please re-enter')
        } else {
            var day = date % 7
            switch (day) {
                case 1:
                    alert('It's Wednesday')
                    break
                case 2:
                    alert('It's Thursday')
                    break
                case 3:
                    alert('It's Friday')
                    break
                case 4:
                    alert('It's Saturday')
                    break
                case 5:
                    alert('This day is Sunday')
                    break
                case 6:
                    alert('It's Monday')
                    break
                case 0:
                    alert('It's Tuesday')
                    break
            }
        }
    </script>

★★ display time case ★★

Common situations in work: second kill countdown

Requirement: the user enters a second and returns xx days, xx hours, xx minutes and xx seconds. And if the converted number is less than 10, fill 0 in front

For example: the user inputs 10000, and we output: 00 days 02:46:40

    <script>
        // User input 10000, we output: 00 days 02:46:40
        // var num = Number(prompt('Please enter the number of seconds: ');
        var num = 10000;
        // second
        var s = num % 60;
        s = s < 10 ? '0' + s : s;

        // minute
        var m = parseInt(num / 60) % 60;  
        // xxx.xxx minutes is converted to digital type. Take the previous integer as minutes. This minute has the relationship from 60 minutes to hours. All% 60 get the remaining minutes
        m = m < 10 ? '0' + m : m;

        // Time
        var h = parseInt(num / 3600) % 24;
        // xxx.xxx hours, converted to numeric type. Take the previous integer, that is, the time. This time has the relationship of 24 hours into the day. All% 24, get the remaining hours
        h = h < 10 ? '0' + h : h;

        // day
        var d = parseInt(num / (3600 * 24));
        // One day = 3600 seconds * 24 hours (one hour = 3600 seconds)
        d = d < 10 ? '0' + d : d;

        alert(d + 'day' + h + 'Time' + m + 'branch' + s + 'second')
    </script>

Tags: Javascript Front-end

Posted by UTAlan on Thu, 05 May 2022 17:45:08 +0300