Some basic exercises about loops

Some knowledge points about circulation

1. while loop structure

 Precautions:

The while loop requires us to declare the variables outside the loop. During the execution of the code, the variables must be changed, otherwise the loop will be useless.
Continue without pause.
A loop that never stops is called an infinite loop. The program never stops, and the CPU usage is getting higher and higher, which can easily cause the system to crash. So we are writing code
Always avoid writing an infinite loop.

2. do while loop structure

3. for loop structure

 

1. Output a multiple of 7 within one hundred or a number with a units digit of 7 or a tens digit of 7.

 // 条件:包含7或7的倍数
    // //     // 包含7的条件 
    // //     // 个位是7- 对10求余,余数是否是7


for (var a = 1; a <= 100; a++) {
      if (a % 7 == 0 || a % 10 == 7 || parseInt(a / 10) == 7) {
             console.log(a)
         }

  }

2,2. The starting salary is 10K, with an annual increase of 5%. How much will the salary be in 50 years?

var money = 10000
     for(var a=1; a<=50; a++) {
         money = money + money * 0.05
     }
     console.log(money);

3. Print all numbers between 100 and 200 that are divisible by 3 or 7

for(var a=100; a<=200; a++) {
      if(a%3 === 0 || a%7 === 0) {
          console.log(a);
      }
    }

4. Find the number of daffodils between 100-999.

 for(var a=100; a<1000; a++) {
        var bai = parseInt(a/100) //得到百位数
        var shi = parseInt(a/10)%10//得到十位数
        var ge = a%10   //得到个位数
        if(bai*bai*bai + shi*shi*shi + ge*ge*ge === a) {
            console.log(a);
        }
    }

5. Find the factorial of 5

var result = 1
    for(var a=5;a>=1;a--) {
        result = result * a
    }
    console.log(result);

*****6. Type out the multiplication table

//double for loop

  document.write('<pre>');
    for (var a = 1; a <= 9; a++) {
        for (var b = 1; b <= a; b++) {
            var result = a * b//得出两个数相乘的结果
            document.write(b + '*' + a + '=' + result + '   ');
        }
        document.write('<br>');
    }

7 Display all leap years from 1000 to 2000 on the page; and break every four digits

 8. Print triangles

  // 打印正三角形核心思想:双重for循环,一个代表行,一个代表列
    document.write('<pre>')
    for (var a = 1; a <= 9; a++) { //代表行
        for (var b = 1; b <= a; b++) { //代表列
            document.write('*')
        }
        document.write('<br>')
    }

right inverted triangle 

    document.write('<pre>')
    for (var a = 9; a >= 1; a--) {
        for (var b = 1; b <= a; b++) {
            document.write('*');
        }
        document.write('<br>');
    }

9. Use a loop to calculate the following results

    1 - 1/2 + 1/3 - 1/4 + ... - 1/100;

/*思路:var a=0;先定义一个存每一次得到的数
    第一次是a=a+1/1;
    第二次是a=a-1/2;
    第三次是a=a+1/3;
    第四次是a=a-1/4;
    奇数为加,偶数为减;
    */
    var a = 0; //定义一个变量存数据
    for (var sum = 1; sum <= 100; sum++) {
        if (sum % 2 == 0) {
            a = a - 1 / sum;
        } else {
            a = a + 1 / sum
        }
    }
    console.log(a)

10, find the factorial sum of all numbers within 20

 var sum = 0; //设置一个变量存所有阶乘后的数相加的值
    for (var a = 1; a <= 20; a++) { //循环一到20的数
        var result = 1; //
        for (var b = 1; b <= a; b++) {
            result = result * b;
        }
        sum = result + sum;
    }
    console.log(sum)

11,

There is a chessboard with 64 squares. The weight of 1 sesame seed placed in the first square is 0.00001kg, 2 sesame seeds are placed in the second square, and 4 is placed in the third square. Find the weight of all sesame seeds placed on the chessboard. , and output how many pieces are placed in each grid;

Idea:

  /*

    1   1

    2   2

    3   4

    4   8

    5   16

    */

    //Finally, we need to find the weight of all sesame seeds, first find the quantity, first find the number of sesame seeds in each square

 // var num = 1 // 第一个方格中数量
    // var sum = 0
    // sum += num
    // for(var a=2; a<=64; a++) {
    //     num = num * 2
    //     console.log('第'+a+'个方格中芝麻数量为'+num);
    //     sum += num
    // }
    // console.log(sum * 0.00001);

    // var sum = 0
    // for(var a=1;a<=64;a++) {
    //     sum += Math.pow(2, a-1)
    // }
    // console.log(sum);

12. A person has a deposit of 50,000 yuan in the bank. The bank charges a service fee every month. When the deposit is more than 5,000 yuan, it charges 5% of the total amount every month. When the total amount is not more than 5,000 yuan, there is no service fee. Assuming that this person never uses it after depositing it, the bank will use a cycle to calculate the service fee. How many times can this person's handling fee be deducted? How much money is left after each deduction?


    var money = 50000; //原本的钱
    var k = 0 //设置一个计数器
    while (money > 5000) {
        var kou = money * 0.05 //每一次扣的钱
        money = money - kou; //剩下的钱
        k++; //计数器
        console.log('第' + k + '次扣完剩下' + money + '钱')
    }
    console.log(k)

13. The monkey picked peaches. The monkey picked x peaches. He ate half of them every day and then one more. On the 7th day, there was only one left. How many peaches did the monkey pick?

   /*
    7   num = 1
    6   num = num+1)*2 = 4
    5   num = num+1)*2 = 10
    4   num = num+1)*2 = 22
    3   46
    2   94
    ... 
    1   190
    */

var sum = 1
    for (var i = 7; i > 1; i--) {
        sum = (sum + 1) * 2
    }
    console.log(sum)

14.

11. There is a ball that bounces half of its height every time it hits the ground. If it is dropped from a height of 10 meters, how much distance will the ball have traveled when it bounces for the tenth time.

Problem-solving ideas:

 To calculate the total distance traveled, you need to calculate the height of each bounce + the height of the fall.

    h = 10

    1   10 + 10/2               h+h/2   h=h/2

    2   10/2 + 10/2/2           h+h/2   h=h/2

    3   10/2/2 + 10/2/2/2

    */

    // 初始高度
    var h = 10
    // 定义所有距离的容器
    var result = 0
    for(var a=1; a<=10; a++) {
        // 计算每次经历的距离
        var s = h + h/2
        // 为了下次还可以使用新的h计算,将弹起的高度重新赋值给h
        h = h/2
        // 将所有的s相加的结果
        result += s
    }
    console.log(result);

15. 12. Output all prime numbers between 100-200 (prime numbers are not divisible by other numbers except 1 and itself)

 // Prime number: 5 2 3 7 11 13 17 19 13

    //How to determine whether a number is prime?

    /*

    19

    19%2

    19%3

    19%4

    19%5

    %6

    ...

    %18

    */

// var num = 101
    // // 体面的着装
    // var k = true
    // // 循环处理重复的求余
    // for(var a=2; a<num; a++) {
    //     if(num%a === 0) {
    //         // 不是素数
    //         k = false // 爬狗洞了,粘上土了
    //         break
    //     } /*else {
    //         // 2 3 4 5 6
    //     }*/
    // }
    // // 循环结束了,将所有数字都除过了,才能知道是素数
    // // 经过判断,看看循环中是否有被整除过的情况
    // // 条件:判断循环中是否被break了
    // // 判断k是体面的,还是粘上土的
    // if(k) {
    //     console.log('是素数');
    // } else {
    //     console.log('不是素数');
    // }

Guess you like

Origin blog.csdn.net/weixin_45441470/article/details/123386748