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

        Today I started to sort out the small JavaScript case of the second part. I originally planned to sort out all articles, but when I searched, I found that if a small partner searches for the keywords of one of the questions, if it is below, it is very difficult to find. .

For example, there are 100 cases in this article, and some friends searched for question 67, which is very difficult to find, so I decided to set 5 cases for each article in the future .

When it is convenient for everyone, I also take it easy. If the article of the younger brother is helpful to you, remember to give it a thumbs up!

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

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

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

Case 1: (6 methods) Comparing the maximum value of two numbers

// - Title description: The user enters 2 values ​​in sequence, and the largest value pops up at the end

// - Topic hint: Pop the maximum value through comparison operators

The ideas of the six methods are:

Method 1: Use an if statement to make a simple judgment in an ideal state

Method 2: Use ternary expressions to complete the corresponding effect

Method 3: Use the switch to exit the definition of the switch. If you understand this point, there must be no problem behind the switch

The following three are based on non-ideal conditions, which are to judge whether the user input is a number, and are closer to the development environment at work.

Method 4: The upgraded version adds a cycle, which can judge that if it is not a number, it needs to be re-entered, and it can also be compared all the time

Method 5: Use isNaN to determine whether the input is a number

Method 6: Use number() to forcibly convert strings into numbers      

Supplementary Note: The last three methods use the knowledge of the next two days, and you can use them flexibly after previewing

// 方法一  用if 语句
    var num1 = +prompt('请输入第一个值:')
    var num2 = +prompt('请输入第二个值:')
    if (num1 > num2) {
        alert('两个数的较大的一个为:' + num1)
    } else {
        alert('两个数的较大的一个为:' + num2)
    }
    // 方法二  用三元表达式
    var num1 = +prompt('请输入第一个值:')
    var num2 = +prompt('请输入第二个值:')
    var max = num1 >= num2 ? num1 : num2;
    alert('两个数的较大的一个为:' + max)
    // 方法三 用switch 做   退出用的switch定义  参考老叶的ATM机

    var num1 = +prompt('请输入第一个值:')
    var num2 = +prompt('请输入第二个值:')
    var flag = true;
    switch (flag) {
        case num1 > num2:
            alert('两个数的较大的一个为:' + num1)
            break;
        case num1 <= num2:
            alert('两个数的较大的一个为:' + num2)
            break;
        default:
            break;
    }

    //方法四 升级版  加入了一个循环  可以判断如果不是数字需要重新输入  也可以一直比较
    var flag = true;
    while (flag) {
        var num1 = +prompt('请输入第一个值:')
        var num2 = +prompt('请输入第二个值:')
        switch (flag) {
            case num1 > num2:
                alert('两个数的较大的一个为:' + num1)
                flag = false;
                break;
            case num1 <= num2:
                alert('两个数的较大的一个为:' + num2)
                flag = false;
                break;
            default:
                alert('您的输入有误,请重新输入数字!')
                break;
        }
    }

    // 方法五
    var flag = true;
    while (flag) {
        var num1 = +prompt('请输入第一个值');
        var num2 = +prompt('请输入第二个值');
        // console.log(typeof num1);
        // if (!isNaN(num1) == true && !isNaN(num2) == true) {
        if (!isNaN(num1) && !isNaN(num1)) {
            // console.log(num1);
            if (num1 > num2) {
                alert('您输入最大的数值为:' + num1);
                flag = false;
            } else {
                alert('您输入最大的数值为:' + num2);
                flag = false;
            }
        } else {
            alert('请输入正确的数值');
            continue;
        }
    }

    // 方法六  
    var num1 = prompt('请输入第一个值');
    var num2 = prompt('请输入第二个值');

    console.log(Number(num1), Number(num2));
    if (Number(num1).toString() == NaN.toString() || Number(num2).toString() == NaN.toString()) {
        alert('NOT OK')
    } else {
        if (num1 > num2) {
            alert('两个值较大的一个为' + num1)
        } else {
            alert('两个值较大的一个为' + num2)
        }
    }
    // ### 3 - 判断奇偶性
    // - 题目描述:
    //   ​	用户输入一个数,判断是奇数还是偶数

    // 方法一  用if 语句

    var num = +prompt('请输入一个数字:')
    if (num % 2 == 0) {
        alert('这是一个偶数')
    } else {
        alert('这是一个奇数')
    }
    // 方法二  用三元表达式
    var num = +prompt('请输入一个数字:')
    var re = num % 2 == 0 ? '偶数' : '奇数';
    alert('这是一个' + re)
    // 方法三 用switch 做   退出用的switch定义  参考老叶的ATM机
    var num = +prompt('请输入一个数字:')
    switch (num % 2) {
        case 0:
            alert('这是一个偶数');
            break;
        case 1:
            alert('这是一个奇数数');
            break;
        default:
            alert('输得什么玩意儿');
            break;
    }

    // 方法三升级  加上while  让它判断持续输出  想出去 可以让flag 为false

    var flag = true;
    while (flag) {
        var num = +prompt('请输入一个数字:')
        switch (num % 2) {
            case 0:
                alert('这是一个偶数');
                flag = false;
                break;
            case 1:
                alert('这是一个奇数数');
                flag = false;
                break;
            default:
                alert('输得什么玩意儿,重新输!');
                break;
        }
    }

Case 2: (3 methods) Treat guests to dinner

    // ​ Receive the amount of money in the monitor's pocket?

    // ​ If it is greater than or equal to 2000, please eat western food.

    // ​ If less than 2000, greater than or equal to 1500, please eat fast food.

    // ​ If less than 1500, greater than or equal to 1000, please drink.

    // ​ If less than 1000, greater than or equal to 500, please eat lollipops.

    // ​ Otherwise, remind the monitor to bring enough money next time

Three ways of thinking:

1. Regular if statement

2. Switch to rewrite

3. Double-layer nesting of ternary expressions 

    // 方法一  常规的if语句 
    var monitorMoney = +prompt('班长口袋中有多少钱:')
    if (2000 <= monitorMoney) {
        alert('班长:我请大家吃西餐!')
    } else if (1500 <= monitorMoney) {//  有个注意点 不能够写成(1500 <= monitorMoney < 2000)    但是可以写成 (1500 <= monitorMoney && monitorMoney< 2000)
        alert('班长:我请大家吃快餐!')
    } else if (1000 <= monitorMoney) {
        alert('班长:我请大家喝饮料!')
    } else if (500 <= monitorMoney) {
        alert('班长:我请大家吃棒棒糖!')
    } else if (monitorMoney < 500) {
        alert('哪怕身上无分文,也要花呗度佳人!')
    }


    // 方法二
    var monitorMoney = +prompt('班长口袋中有多少钱:')
    if (monitorMoney > 0) {
        var num1 = parseInt(monitorMoney / 500)
        switch (num1) {
            case 0:
                alert('哪怕身上无分文,也要花呗度佳人!')
                break;
            case 1:
                alert('班长:我请大家吃棒棒糖!')
                break;
            case 2:
                alert('班长:我请大家喝饮料!')
                break;
            case 3:
                alert('班长:我请大家吃快餐!')
                break;
            default:
                alert('班长:我请大家吃西餐!')
                break;
        }
    } else {
        alert('你真是个老穷批')
    }

// 方法三  如果非要用三元表达式,也能做,不过在if中嵌套,而且得用两次三元,毕竟一次三元只出两个结果,而且三元出的是个值,还得用取整   不推荐  大家可以自己摸索一下,  也可以给我留言大家一块整整

Case 3: (2 ways of writing) Receive the user name and password entered by the user

If the user name is admin and the password is 123456 , it will prompt the user to log in successfully. If it is incorrect, it will prompt that the account number or password is wrong, please re-enter the password again

Method 1: Double-layer nesting of ternary expressions (Look at this case, you can deepen your understanding of ternary expressions)

Method 2: Normal ternary expression + logical AND operator

    var myName = prompt('请输入用户名:')
    var password = prompt('请输入密码:')
    var re = myName == 'asd' ? (password == '123' ? '成功登录' : '密码错误') : '密码名错误'
    console.log(re);
    alert(re)

    // 方法二  三元运算符 判断两个条件
    var name = prompt('请输入姓名')
    var password = prompt('请输入密码')
    var result = name == 'aaa' && password == '123' ? 'ok' : 'not ok'
    console.log(result);
    alert(result)

Case 4: (4 ways of writing) Score conversion, give a score, and judge the grade. Greater than or equal to 90 A, greater than or equal to 80 and less than 90 B, greater than or equal to 70 and less than 80 C, greater than or equal to 60 and less than 70 D, less than 60 E 

Method 1: Conventional if writing

Method 2: The upgraded version adds a cycle, more proficient in the cycle

Method 3: Use a switch to write a few more cases. Ideally, the input is a score, which is directly converted into a number

Method 4: Upgraded version. Take all factors into account. Is it a number? Do you need to loop? How to exit?

// 方法一  常规
    var score = prompt('请输一个分数:')
    if (0 <= score && score <= 100) {
        if (score >= 90) {
            alert('您的等级是A')
        } else if (score >= 80) {
            alert('您的等级是B')
        } else if (score >= 70) {
            alert('您的等级是C')
        } else if (score >= 60) {
            alert('您的等级是D')
        } else {
            alert('您的等级是E')
        }
    } else {
        alert('您的输入有误,青重新输入')
    }

    // 方法一  升级版

    var flag = true;
    while (flag) {
        var score = prompt('请输一个分数:\n 如果退出请输入:退出')
        if (0 <= score && score <= 100) {
            if (score >= 90) {
                alert('您的等级是A')
            } else if (score >= 80) {
                alert('您的等级是B')
            } else if (score >= 70) {
                alert('您的等级是C')
            } else if (score >= 60) {
                alert('您的等级是D')
            } else {
                alert('您的等级是E')
            }
        } else if (score == '退出') {
            flag = false;
        } else {
            alert('您的输入有误,青重新输入')
        }
    }


    // 方法三 用switch  做   多写几个case  理想状态下  输入的是分数,直接转成数字型
    var score = +prompt('请输一个分数:\n 如果退出请输入:退出')
    var num = parseInt(score / 10)
    switch (num) {
        case 10:
            alert('您的等级是A')
            break;
        case 9:
            alert('您的等级是A')
            break;
        case 8:
            alert('您的等级是B')
            break;
        case 7:
            alert('您的等级是C')
            break;
        case 6:
            alert('您的等级是D')
            break;
        default:
            alert('您的等级是E')
            break;
    }


    // 方法四  升级版  把所有因素都考虑进去 是不是数字,需不需要循环,多次查询  怎样退出
    var flag = true;
    while (flag) {
        var score = prompt('查询器\n 请输一个分数:\n 如果退出请输入:退出')
        if (0 <= score && score <= 100) {
            var num = parseInt(score / 10)
            switch (num) {
                case 10:
                    alert('您的等级是A')
                    break;
                case 9:
                    alert('您的等级是A')
                    break;
                case 8:
                    alert('您的等级是B')
                    break;
                case 7:
                    alert('您的等级是C')
                    break;
                case 6:
                    alert('您的等级是D')
                    break;
                default:
                    alert('您的等级是E')
                    break;
            }

        } else if (score == '退出') {
            flag = false;
        } else {
            alert('您的输入有误,青重新输入')
        }
    }

Case 5: (2 ways of writing)

The lucky number of the lottery is 6. The user has three chances. Enter 6 within three times, and the prompt congratulations won’t pop up. If it fails three times, it will fail.

Method 1: Do not do it in a loop (exercise our if logic ability)

Method 2: Do it with a loop (you can write all the three loops of if while do...while, it is always good to have more beginners)

    // 不用循环做
    var lucky1 = prompt('你有三次机会,请输入数字');
    if (lucky1 == 6) {
        alert('恭喜你中奖')
    } else if (lucky1 != 6) {
        var lucky2 = prompt('你还有两次机会,请输入数字');
        if (lucky2 != 6) {
            var lucky3 = prompt('你只有一次机会,请输入数字');
            if (lucky3 != 6) {
                alert('非常遗憾')
            } else {
                alert('恭喜你中奖')
            }
        } else {
            alert('恭喜你中奖')
        }
    }

    // 用循环做
    var lucky1 = prompt('你有三次机会,请输入数字');
    for (var i = 2; i >= 1; i--) {
        if (lucky1 == 6) {
            alert('恭喜你中奖')
            break;
        } else {
            var lucky3 = prompt('你还有' + i + '次机会,请输入数字');
        }
    }

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/124100786