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

It’s been a long time since I’ve updated it. To be honest, I’ve learned a lot, and I’m a cursive. I want to learn everything faster, so I don’t have time to update. In the past few days, I will sort out some small cases one after another. Practice your hands, each question has several ways of writing

Super friendly for novices! ! !

After all, I am also a novice! ! !

Without further ado, let's get to the dry goods!

Case 1; The user enters the prompt box to enter the year of birth and how old the document is printed! (3 ways of writing)

For this question, three ways of writing have been sorted out. The title is very simple. The three ways of writing are mainly to let everyone familiarize themselves with the differences of each method. 

They are:

1. The most conventional pop-up window

2. Added the judgment condition for whether the content entered by the user is a number, and at the same time use a while loop until the input is correct

3、isNaN  + while

    // 方法一  最简单常规理想状态
    var year = prompt('请输入年份:');
    age = 2022 - year;
    alert('你已经' + age + '岁了');


    // 方法二  复杂一点
    var flag = true;
    while (flag) {
        var year = +prompt('请您输入您的出生年份') // 前面写个 +  是把字符强制转成数字型
        if (typeof year === 'number') {
            var age = 2022 - year; // year 取过来的是字符串型  但是这里用的减法 有隐式转换
            alert('您今年已经' + age + '岁了');
            flag = false;
        } else {
            alert('请输入正确的数字类型');
            continue;
        }
    }

    // 方法三  用的isNaN
    var year = prompt('请您输入您的出生年份');
    var flag = isNaN(year);
    while (flag) {
        alert('请输入正确的数字类型');
        year = prompt('请您输入您的出生年份');

    }
    alert('您年龄是:' + (2022 - year));

Case 2: The input box (prompt) pops up twice to calculate the sum of the two times!

This is a normal operation, taking values ​​twice, plus implicit conversion of numbers Because the data type received by prompt is a string, implicit conversion is required!

    var num = prompt('请输入第一个值');
    var num1 = prompt('请输入第二个值');
    num = num - 0;
    num1 = num1 - 0;
    alert('两个数的和为' + parseFloat(num + num1))

Case 3: [Difficulty ★★★★] If today is Saturday, what day of the week is it in 1000 days? (4 ways of writing)

1. Normal modulo operation

2. Although the switch classification discussion is a bit long, the thinking is very clear and easy to understand

3. Writing ternary expressions

4. Simple numbers

    // 方法一  默认今天是第一天  直接取余 
    var num = 1000 % 7 - 1;
    console.log('是的星期' + num + '啊');
    // 方法二  升级版  虽然有点长,不过思路很简单
    var time = prompt('今天是周六,你想知道多少天后是星期几吗?')
    time = time - 0;
    var time = time % 7;
    switch (time) {
        case 0:
            alert('星期六');
            break;
        case 1:
            alert('星期日');
            break;
        case 2:
            alert('星期一');
            break;
        case 3:
            alert('星期二');
            break;
        case 4:
            alert('星期三');
            break;
        case 5:
            alert('星期四');
            break;
          default:
            alert('星期五');
    }
    // 方法三  三元表达式
    var newTime = +prompt('请输入您要查询的日期(可以显示星期几):')
    var num = newTime % 7  // 必须得加一个结果 不然alert  没法调用结果
    var re = newTime % 7 < 3 ? num + 5 : num - 2;
    alert(newTime + '号是星期' + re)

    // 方法四   简单数字类
    var newTime = +prompt('请输入您要查询的日期(可以显示星期几):')
    var num = newTime % 7;
    if (num < 6) {
        alert(newTime + '号是星期' + (num + 2))
    } else {
        alert(newTime + '号是星期' + (num - 5))
    }   

Case 4:

[Difficulty ★★★★★] The user enters a three-digit number, and the program calculates the sum of the three-digit number (realized in two ways).

    // for example:    

    // When the user enters 155, 11 will pop up

    // When the user enters 316, 10 pops up

    // When the user enters 989, 26 will pop up

    // When the user enters 678, 21 pops up

    // Note: The validity and legality of the numbers entered by the user do not need to be considered. For example, if the user enters 34343, "I love you", we don't need to consider this situation, just imagine that the user will be very good and will definitely enter 3 digits. This is because we did not learn the if statement.

This topic is mainly to have a clear idea, here are two ways of writing

The core is, how to extract the numbers in the ones, tens, and hundreds respectively?

    alert('方法一')
    var num5 = prompt('请输入一个三位数')

    num.substring

    var num6 = num5[0] - 0;
    var num7 = num5[1] - 0;
    var num8 = num5[2] - 0;
    var num9 = num8 + num6 + num7;
    alert('三位数相加等于' + num9);


    alert('方法二')
    var num5 = prompt('请输入一个三位数')
    var num5 = num5 - 0;
    var num6 = num5 % 10;//个位数           3
    var num7 = num5 % 100;
    num7 = parseInt(num7 / 10);//十位数      2.3  2       
    var num8 = parseInt(num5 / 100);//百位数
    var num9 = num8 + num6 + num7;
    alert('个位数:' + num6);
    alert('十位数:' + num7);
    alert('百位数:' + num8);
    alert('三位数相加等于' + num9);

Case 5: There are 3 ways of writing this question, which are the basic class, the upgraded version, and the working version

    // ###1- Determine the time period.

    // - Title description:

    // The user enters a few points to pop up a greeting message;

    // If the user enters 12:00 noon;

    // The user enters 18 o'clock and pops up good evening;

    // ​ The user enters 23 o'clock and pops up late at night;

    // 方法一  硬性的几个答案
    var time = prompt('请输入时间点:')
    if (time == 12 || time == '12点') {
        alert('中午好!')
    } else if (time == 18 || time == '18点') {
        alert('傍晚好!')
    } else if (time == 24 || time == '24点') {
        alert('晚上好!')
    }

    // 方法二  输入时间段问好

    var now = +prompt('请输入时间点:')
    if (now > 0 && now <= 6) {
        alert('午夜好!')
    } else if (now > 6 && now <= 11) {
        alert('早上好!')
    } else if (now > 11 && now <= 14) {
        alert('中午好!')
    } else if (now > 14 && now <= 18) {
        alert('下午好!')
    } else {
        alert("晚上好!");
    }


    // 方法三  实时时间问好。访问现在电脑时间,跳出信息框,大网站基本上都有这种定点提示框
    var now = (new Date()).getHours();
    if (now > 0 && now <= 6) {
        alert('午夜好!')
    } else if (now > 6 && now <= 11) {
        alert('早上好!')
    } else if (now > 11 && now <= 14) {
        alert('中午好!')
    } else if (now > 14 && now <= 18) {
        alert('下午好!')
    } else {
        alert("晚上好!");
    }

I have sorted out 5 cases today, if it is useful to you, remember to like it!

In the future, various practical cases will be updated for your reference and practice, and there are various solutions, remember to bookmark it!

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

 

Guess you like

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