Day03 Some small cases about the basics of JavaScript (multiple methods to solve problems)

Today I started to sort out the small JavaScript cases in the third part. The [day series] I recently sorted out is a pure example series . It also increases the proficiency of various methods and improves the code speed by typing a few more lines of code.

Another series will be updated in two days. I haven’t decided on the name yet. Let’s talk about some different and easy-to-understand concept definitions in each series. But the content is already being prepared, if you are interested, add a follower to not get lost!

Without further ado, let's go to today's dry goods!

Not much to say, let's do the dry goods today!

Not much to say, let's do the dry goods today!

Case 1: (3 ways of writing) find the sum of all numbers divisible by 3 and 7 within 100

1. Use if while do...while to write it again to increase the understanding of the code

// 方法一 常规if语句
    var sum = 0;
    for (var i = 1; i <= 100; i++) {
        if (i % 3 == 0 && i % 7 == 0) {
            sum += i;
        }
    }
    console.log(sum);

    // 方法二 用while  不过i++ 别放错位置
    var sum = 0;
    var i = 1;
    while (i <= 100) {
        if (i % 3 == 0 && i % 7 == 0) {
            sum += i;
        }
        i++
    }
    console.log(sum);

    // 方法三  do while
    var sum = 0;
    var i = 1;
    do {
        if (i % 3 == 0 && i % 7 == 0) {
            sum += i;
        }
        i++
    } while (i <= 100)
    console.log(sum);

Case 2: Find all the numbers between 1-100 that can be divisible by 3 and 7 at the same time, and calculate the cumulative sum

1. Still use if while do...while to write it again

// 方法一  常规for 循环
    var str = '';
    var sum = 0;
    for (var i = 1; i <= 100; i++) {
        if (i % 3 == 0 && i % 7 == 0) {
            str += i + '\t';
            sum += i;
        }
    }
    console.log(str);
    console.log(sum);



    // 方法二  用while试试   用while的时候 注意i   需要定义
    var str = '';
    var sum = 0;
    var i = 1;
    while (i <= 100) {
        if (i % 3 == 0 && i % 7 == 0) {
            str += i + '\t';
            sum += i;
        }
        i++;
    }
    console.log(str);
    console.log(sum);

    // 方法三  用do  while 
    var str = '';
    var sum = 0;
    var i = 1;
    do {
        if (i % 3 == 0 && i % 7 == 0) {
            str += i + '\t';
            sum += i;
        }
        i++;
    } while (i <= 100)
    console.log(str);
    console.log(sum);

Case 3: (4 ways of writing) use for loop to print triangles

    // - Title description:

    // - Print the shape at one time on the console as follows:

    //     ☆

    //     ☆☆

    //     ☆☆☆

    //     ☆☆☆☆

    //     ☆☆☆☆☆

Analysis: There are many methods, but the logic must be clarified. The first layer of for manages the number of lines, and the second layer of for manages the number , so the key is to understand that after the first layer of for is executed, a newline needs to be added . The second for controls the number

1. Normal if loop

2. Change the initial value of i, and learn to flexibly rewrite the initial value of i. Some students struggle to set the initial value of i to 0 or 1. In fact, as long as this can meet the needs, you can set either one, and there are no hard and fast rules.

3. A while double loop is used to rewrite in the middle. You can use this case to increase your understanding of the use of while double loops

 // if  语法
    // 写法一
    var str = '';
    for (var i = 1; i <= 10; i++) {
        for (var j = 1; j <= i; j++) {
            str += '★'
        }
        str += '\n'
    }
    console.log(str);

    // 写法二
    var str = '';
    for (var i = 10; i >= 1; i--) {
        for (var j = i; j <= 10; j++) {
            str += '★'
        }
        str += '\n'
    }
    console.log(str);
    // 用while  双层套用
    var str = '';
    var i = 1, j = 1;
    while (i <= 10) {
        j = 1   // 关键在这
        while (j <= i) {

            str += '★'
            j++
        }
        str += '\n'
        i++
    }
    console.log(str);

    // 写法二 
    var str = '';
    var i = 10, j = 1;
    while (i >= 1) {
        j = i
        while (j <= 10) {
            str += '★'
            j++
        }
        str += '\n'
        i--
    }
    console.log(str);

Case 4: (2 ways of writing) use for loop to print 99 multiplication table

1. Use a regular double for loop to write  

2. Use the double while loop   to rewrite. Through this case, familiarize yourself with the nesting rules of the double while

//   - 利用双重for循环
    var str = '';
    for (var i = 1; i <= 10; i++) {
        for (var j = 1; j <= i; j++) {
            str += j + 'x' + i + '=' + i * j + '\t'
        }
        str += '\n'
    }
    console.log(str);

    //   - 利用双重while循环

    var str = '';
    var i = 1, j = 1;
    while (i <= 10) {
        j = 1   // 关键在这
        while (j <= i) {
            str += j + 'x' + i + '=' + i * j + '\t'

            j++
        }
        str += '\n'
        i++
    }
    console.log(str);

Case 5: (2 ways of writing) Find the cumulative sum of numbers whose single digit is not 3 between 1-100.

This is relatively simple, write one, just know the idea, and rewrite other methods at will

    // 思路一  用continue
    var sum = 0;
    for (var i = 1; i <= 10; i++) {
        if (i % 10 == 3) {
            continue;
        }
        sum += i;
    }
    console.log(sum);

    var sum = 0;
    for (var i = 1; i <= 10; i++) {
        if (i % 10 != 3) {
            sum += i;
        }
    }
    console.log(sum);

Case 6 (send a question): (2 ways of writing) the 35th integer divisible by 7 and 3

1. Write it with for and while

    var sum = 0;
    var str = '';
    for (var i = 1; i <= 35; i++) {
        if (i % 3 == 0 || i % 7 == 0) {
            str += i + '\t'
            sum += i;
            console.log('第' + i + '是能被3或7整除的数')
        }
    }
    console.log(str);
    console.log(sum);

    // 方法一  while循环 嵌套if

    var sum = 0;
    var str = '';
    var i = 1;
    while (i <= 35) {
        if (i % 3 == 0 || i % 7 == 0) {
            str += i + '\t'
            sum += i;
            console.log('第' + i + '是能被3或7整除的数')
        }
        i++
    }
    console.log(str);
    console.log(sum);

        This is the end of today’s 5 example questions. The content of the example questions is inevitably a bit boring. After all, the veterans who searched for this article also wanted to have a look at the writing method and learn from each other. There are not many points to discuss, so I didn’t add too much There are many narrations in it.

        Next, a series of problem discussions will be launched. Welcome everyone to come to communicate and discuss  some difficult to understand or problem-prone parts encountered in the various front-end learning processes  !

        Click to pay attention to not get lost, I also just started writing CSDN, if this article helps you!

I also ask for a wave of attention here, so I won't get lost next time!

Thank you thank you~~~you~you~you~le~~

 

Guess you like

Origin blog.csdn.net/fightingLKP/article/details/124137742