Relational operators and loop exercises in JavaScript

Relational operator
Like other operators, the following rules should be followed when relational operators operate on non numeric values:
(1) If both operands are numeric, the values are compared
(2) If both operands are strings, compare the character encoding values corresponding to the two strings:
If two single characters are compared, the ASIIC code value of the character is directly compared
Compare bit by bit. If you compare the size, you can get the result directly
(3) If one of the two operands is a value, convert the other to a value and compare the values

    <script>
        alert("A"<"a");//true
        alert("abcd">"adc");//false
        alert(1==true);//true
        // be careful
        alert(Number(null));//0
        alert(Number(undefined));//NAN
        alert(null==undefined);//true
    </script>

Branch statement
Code specification:
1. Operators must be preceded by spaces
2,; Or. Only need to be followed by a space
3. Code indent

    <script>
        var year = 2020;
        if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0){
            alert("This is a leap year:" + year);
        }else{
            alert("This is a normal year:" + year);
        }
    </script>

Omit break

    <script>
        var month = 4;
        var year = 2000;
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                alert("31 day");
                break;
            case 2:
                if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0){
                    alert("29 day");
                }else{
                    alert("28 day");
                }
                break;
            default:
                alert("30 day");
                break;
        }
    </script>


multiplication table

    <script>
        //i is OK
        for(var i = 1; i <= 9; i++){
            //j is the column
            for(var j = 1; j <=i; j++){
                doucument.write(j + "X" + i +"=" + (i*j) + "&nbsp");
            }
            doucument.write("<br/>");
        }
    </script>

triangle

    <script>
        for(var i = 1; i <= 5; i++){
            //Number of spaces
            for(var k = 1; k <= 5-i; k++){
                document.write("1");
            }
            //
            for(var j = 1; j <= i; j++)
            {
                document.write("2");
            }
            document.write("<br/>");
        }
    </script>


Find all the daffodils, three digits, each cube and the sum is equal to the number itself.
153=1 ^3 + 5 ^ 3 + 3 ^ 3
Math.pow(x,y); Find the Y power of X

  <script>
        for(var i=100;i<1000;i++)
        {
            //Each bit is taken out
            var a=i%10;
            var b=parseInt(i/10)%10;
            var c=parseInt(i/100);
            //Cube
            var sum=Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3);
            if(i==sum){
                document.write(i+"<br/>");
            }
        }
    </script>


Enter two numbers and find the maximum common divisor of the two numbers (the maximum number that can divide the two numbers at the same time)
Idea: first find the smallest of the two numbers
The maximum common divisor, the minimum is 1, and the maximum can only be the smaller of the two numbers

    <script>
        var num1=12;
        var num2=6;
        var min=num1>num2?num2:num1;
        while(1){
            if(num1%min==0&&num2%min==0){
                break;
            }
            min--;
        }
        alert("Maximum common divisor of both: "+ min);
    </script>


Enter two numbers and find the least common multiple of the two numbers (the smallest decimal that can be divided by two numbers at the same time)
Idea: first find the maximum number of two numbers
Maximum number + +, until you find the number that can be divided by two numbers (exit the loop)
Least common multiple: the product of the two largest numbers, and the larger of the two smallest numbers

          <script>
              var num1=6;
              var num2=12;
              var max=num1>num2?num1:num2;
              while(1){
                  if(max%num1==0&&max%num2==0){
                      break;
                  }
                  max++;
              }
              alert("Least common multiple of both:"+max);
          </script>


Enter two numbers n,a, if n=3,a=2;
Output the value of 2 + 22 + 222( (no output formula)
If n=4,a=3;
Output 3 + 33 + 333 + 3333
Idea: n=3, add three times, and each addition is one more than the previous addition
The value of the multiple digits each time is a

          <script>
              var n=4;
              var a=3;
              var tmp=a;//tmp is the number accumulated each time
              var sum=0;
              for(var i=0;i<n;i++){
                  sum+=tmp;
                  tmp=tmp*10+a;
              }
              alert(sum);
          </script>


In the five digits, the symmetrical number becomes the palindrome number. Find out all the palindromes, such as 12321
Idea: first find out the first and fifth digits and compare them
Compare the second and fourth digits of the five digits

          <script>
              for(var i=10000;i<100000;i++){
                  var a=i%10;//Bit
                  var b=parseInt(i/10)%10;//Ten
                  var c=parseInt(i/1000)%10;//Thousand bit
                  var d=parseInt(i/10000)%10;//Wan Wei
                  if(a==d&&b==c){
                      document.write(i+"<br/>");
                  }
              }
          </script>


Enter year, month and day to calculate the day (week) of a year

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- Enter year, month and day to calculate the day (week) of a year -->
    <script>
        var year=2008;
        var month=2;
        var date=8;
        var sum=0;//Record total days
        switch(month){
            case 12:
                sum+=30;
            case 11:
                sum+=31;
            case 10:
                sum+=30;
            case 9:
                sum+=31;
            case 8:
                sum+=31;
            case 7:
                sum+=30;
            case 6:
                sum+=31;
            case 5:
                sum+=30;
            case 4:
                sum+=31;
            case 3:
                sum+=28;
                if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0){
                    sum+=1
                }
            case 2:
                sum+=31;
            case 1:
                sum+=date;
                break;
            default:date
                alert("error");
                break;
        }
        alert("This is the second "+ sum + "day");
       // var week = parseInt(sum/7)+1;
       var week=Math.ceil(sum/7);
        alert("This is the second "+ week + "week");
    </script>
</head>
<body>
    
</body>
</html>


Keywords: Javascript

Added by wazza on Sun, 05 Sep 2021 08:00:19 +0300