Some problems solved by JS using loops

1. Print multiples of 3 in 0-100

  • Idea: first use the for loop to print the number from 0 to 100, then use the if conditional statement to filter, and output the ones that can be divisible by 3
for(var i=0;i<=100;i++){
    if(i%3==0&&i!=0){
        console.log(i);
    }
}

2. Write the leap year in 1000-2000 on the page

  • Idea: first use the for loop to print 1000-2000 years, and then use the conditions to determine whether it is a leap year (divisible by 4 and not divisible by 100 or divisible by 400) to filter out leap years for printing
for(var i=1000;i<=2000;i++){
    if(i%4==0&&i%100!=0 || i%400==0){
         console.log(i+'是闰年');
    }
}

3. Print the sum of all even numbers within 100

  • Idea: First use the loop to print out the numbers from 0 to 100, then use the if conditional judgment statement to print out the multiples of 2 (0 is a special even number), and then add up these numbers
var sum=0
    for(var i=1;i<=100;i++){
        if(i%2==0){
            sum+=i
       	}
    }
console.log(sum);
  • Result: 2550

4. Find the sum of 1-1/2+1/3-1/4...1/100

Idea: All numerators remain unchanged and are all 1. When the denominator is an even number, the previous number is subtracted from the even number

var sum=0
for(var i=1;i<=100;i++){
    if(i%2==0){
        sum-=1/i
    }else{
        sum+=1/i
    }
}
console.log(sum);
  • Result: 0.688172179310195

5. Print the triangle

5.1 Ordinary triangles

for (var i = 1; i <= 9; i++) {
            for (var j = 1; j <= i; j++) {
                document.write('☆');
            }
            document.write('<br>')
        }

display effect:
insert image description here

5.2 Inverted triangle

 for (var i = 1; i <= 9; i++) {
            for (var j = 9; j >= i; j--) {
                document.write('☆');
            }
            document.write('<br>')
        }

display effect:
insert image description here

5.3 Isosceles triangle

for (var i = 1; i <= 9; i++) {
    for (var j = 9; j >= i; j--) {
            document.write('&nbsp');
       }
        for (var j = 1; j <= i; j++) {
            document.write('☆');
      }
           document.write('<br>')
    }

display effect:
insert image description here

6. Isosceles trapezoid

  • A trapezoid is achieved by controlling the initial value of i
 for (var i = 5; i <= 9; i++) {
            for (var j = 9; j >= i; j--) {
                document.write('&nbsp');
            }
            for (var j = 1; j <= i; j++) {
                document.write('☆');
            }
            document.write('<br>')
        }

display effect:
insert image description here

7. Output all prime numbers between 100-200

  • Idea: First of all, a prime number is a number that can only be divisible by 1 and itself. We can exclude 1 and itself when looping, and then use double for loops. The outer loop is used to traverse the numbers from 100 to 200, and then define a flag The initial value is true, and then the inner loop is used to determine whether it is a prime number. When it is not a prime number, the flag is assigned a value of false, and then when the flag is true, it is a prime number and output
for(var i=100;i<=200;i++){
            var flag=true
            for(var j=2;j<i;j++){
                if(i%j==0){
                    flag=false
                }
            }
            if(flag){
                document.write(i+'<br>')
            }
        }
  • result:
    insert image description here

8. Find the value of 1+2!+3!+…+20!

  • Idea: This is to find the factorial of 1 plus 2 plus the factorial of 3 plus 4 to the factorial of 20. First, we define a variable sum to receive it, and then write the outer loop to traverse 1 to 20.
  • first cycle
    • When i is equal to 1, k is equal to 1, j is equal to 1, k is equal to k*j is equal to 1, sum is equal to 0 plus 1 is equal to 1
  • second cycle
    • When i equals 2, k equals 1, j equals 1, k equals k*j equals 1
    • j equals 2, k equals k*j equals 2, sum equals 1 plus 2 equals 3
  • third cycle
    • When i equals 3, k equals 1, j equals 1, k equals k*j equals 1
    • j is equal to 2, k is equal to k*j is equal to 2
    • j equals 3, k equals k*j equals 6, sum equals 1 plus 1+2+6=9
  • and so on. . . .
var sum = 0;
        for (var i = 1; i <= 20; i++) {
            var k = 1;
            for (var j = 1; j <= i; j++) {
                k *= j;
            }
            sum += k;
        }
        console.log(sum);

Result: 2561327494111820300

9. There is a chessboard with 64 squares. Put 1 sesame in the first square and the weight is 0.00001kg, put 2 in the second, and put 4 in the third. All the sesame seeds on the board weight

  • Idea: The first one has 1, the second has 2, the third has 4, the fourth has 8, and so on, then the multiplication of the 2 numbers is equal to the third number, respectively define sum and num are used to hold the total quantity and the number, and then use the loop to execute the code 1 to 64
var sum=0;
      var num=1;
      for(var i=1;i<=64;i++){
          sum+=num;
          num*=2;
     }
console.log(sum*0.00001);

Result: 184467440737095.53

10. Complete the page with a nine-nine multiplication table

for (var i = 1; i <= 9; i++) {
            for (var j = 1; j <= i; j++) {
                document.write('<span>'+j+'*'+i+'='+i*j+'</span>');
            }
            document.write('<br>')
        }

Display effect: the style is written separately
insert image description here

11. Print a table with 3 rows and 5 columns

document.write('<table>')
        for(var i=1;i<=3;i++){
            document.write('<tr>')
            for(var j=1;j<=5;j++){
                document.write('<td></td>')
            }
            document.write('</tr>')
        }
        document.write('</table>')

Display effect: the style is added separately
insert image description here

Guess you like

Origin blog.csdn.net/liu0218/article/details/126510806