day19-JavaScript 练习 自增自减练习、条件判断语句练习、条件分支语句练习、补充质数练习、质数练习改进、函数练习

这篇博客主要介绍了JavaScript的自增自减练习,条件判断语句的运用,包括不同成绩的处理、数字排序,以及条件分支语句的实现,如奇数和、7的倍数等。此外,还探讨了质数的判断和补充练习,包括99乘法表和质数之和。最后,博主分享了函数的应用,涉及偶数判断、计算面积和输出个人信息的函数设计。
摘要由CSDN通过智能技术生成

自增自减练习

自增
自增
自减自减

条件判断语句练习

prompt()可以弹出一个提示框,返回是String类型;

从键盘输入不同成绩待遇不同

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var cehngji = prompt();
        if (100 <= cehngji <= 0) {
            if (cehngji == 100) {
                alert("奖励别墅");
            } else if (cehngji >= 90) {
                alert("奖励车");
            } else if (cehngji >= 60) {
                alert("奖励资料书");
            } else if (cehngji >= 0) {
                alert("奖励暴打一顿");
            } else {
                alert("非法输入");
            }
        }
    </script>
</head>

<body>

</body>

</html>

输入三个数排序并输出

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var shu1 = +prompt();
        var shu2 = +prompt();
        var shu3 = +prompt();
        if(shu1 < shu2 && shu1 < shu3){
            if(shu2 < shu3){
                console.log(shu1 + " " + shu2 + " " + shu3)
            }else if(shu2 > shu3){
                console.log(shu1 + " " + shu3 + " " + shu2)
            }
        }else if(shu2 < shu1 && shu2 < shu3){
            if(shu1 < shu3){
                console.log(shu2 + " " + shu1 + " " + shu3)
            }else if(shu1 > shu3){
                console.log(shu2 + " " + shu3 + " " + shu1)
            }
        }else if(shu3 < shu1 && shu3 < shu2){
            if(shu2 < shu1){
                console.log(shu3 + " " + shu2 + " " + shu1)
            }else if(shu2 > shu1){
                console.log(shu3 + " " + shu1 + " " + shu2)
            }
        }
    </script>
</head>

<body>

</body>

</html>

同时满足、有真、都不

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var shu1 = +prompt("身高(cm)");
        var shu2 = +prompt("财富(万元)");
        var shu3 = +prompt("颜值(分)");
        if(shu1 > 180 && shu2 > 1000 && shu3 > 80){
            console.log("太帅了");
        }else if(shu1 > 180 || shu2 > 1000 || shu3 > 80){
            console.log("一般吧");
        }else{
            console.log("丑");
        }
    </script>
</head>

<body>

</body>

</html>

条件分支语句练习

(for,while)

>60合格,<60不合格

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        
        while(true){
            var cehngji = +prompt("输入成绩(0-100)");
            if(0 <= cehngji && cehngji <= 100){
                break;
            }
        }
        if (100 <= cehngji <= 0) {
            if (cehngji == 100) {
                alert("奖励别墅");
            } else if (cehngji >= 90) {
                alert("奖励车");
            } else if (cehngji >= 60) {
                alert("奖励资料书");
            } else if (cehngji >= 0) {
                alert("奖励暴打一顿");
            } else {
                alert("非法输入");
            }
        }
    </script>
</head>

<body>

</body>

</html>

打印1-100奇数之和

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>

        for (var i = 1, he = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                he += i;
            }
        }
        console.log(he);
    </script>
</head>

<body>

</body>

</html>

1-100之间所有7的倍数的个数及总和

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>

        for (var i = 1, he = 0, j = 0; i <= 100; i++) {
            if (i % 7 == 0) {
                he += i;
                j++
            }
        }
        console.log("和:" + he);
        console.log("个数:" + j);
    </script>
</head>

<body>

</body>

</html>

水仙花数

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        for (var i = 100; i < 1000; i++) {
            // console.log(parseInt(i/100));取出百位数字
            // console.log(parseInt(i/10%10));取出十位数字
            // console.log(parseInt(i%10));取出个位数字
            if(parseInt(i/100)*parseInt(i/100)*parseInt(i/100)+parseInt(i/10%10)*parseInt(i/10%10)*parseInt(i/10%10)+parseInt(i%10)*parseInt(i%10)*parseInt(i%10)==parseInt(i/100)*100+parseInt(i/10%10)*10+parseInt(i%10)){
                console.log(i);
            }
        }
    </script>
</head>

<body>

</body>

</html>

接收一个数字判断是否是质数

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var shu = prompt("输入一个数");
        var zhuangtai = true;
        for (var i = 2; i < shu; i++) {
            if(shu % i == 0){
                zhuangtai = false;
            }
        }
        if(zhuangtai){
            console.log(shu + "是一个质数");
        }else{
            console.log("不是质数");
        }
    </script>
</head>

<body>

</body>

</html>

补充质数练习

嵌套的for循环
"<span>" "</span>"

打印99乘法表

99乘法表

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        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>");
        }
    </script>
    <style>
        span{
            display: inline-block;
            width: 100px;
        }
    </style>
</head>

<body>

</body>

</html>

打印1-100之间所有质数之和

质数之和

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var he = 0;
        for (var j = 2; j <= 100; j++) {
            var zhuangtai = true;
            for (var i = 2; i < j; i++) {
                if (j % i == 0) {
                    zhuangtai = false;
                }
            }
            if (zhuangtai) {
                document.write(j + ",");
                he += j;
            }
        }
        document.write("he:" + he);
    </script>
</head>

<body>

</body>

</html>

质数练习改进

Math.sqrt()对一个数进行平方;
质数练习改进

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var he = 0;
        console.time("jishi");
        for (var j = 2; j <= 100; j++) {
            var zhuangtai = true;
            // 使用Math.sqrt()进一步提高效率
            for (var i = 2; i <= Math.sqrt(j); i++) {
                if (j % i == 0) {
                    zhuangtai = false;
                    // 提高效率,节省时间,直接结束无用的循环
                    break;
                }
            }
            if (zhuangtai) {
                document.write(j + ",");
                he += j;
            }
        }
        console.timeEnd("jishi");
        document.write("he:" + he);
    </script>
</head>

<body>

</body>

</html>

函数练习

判断数是否是偶数,返回true

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var shu = prompt("输入一个数");
        function hanshu(){
            return (shu%2==0);
        }
        var jieguo = hanshu();
        alert(jieguo);
    </script>
</head>

<body>

</body>

</html>

根据半径算面积,并返回计算结果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        var shu = prompt("输入半径:");
        function hanshu(){
            return (3.14*shu*shu);
        }
        var jieguo = hanshu();
        alert(jieguo);
    </script>
</head>

<body>

</body>

</html>

创建一个函数,在控制台输出一个人的信息(传参、封装对象)

传参

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        function hanshu(name, age, shenfen){
            return (name + age + "岁了," + "是一个" + shenfen);
        }
        var jieguo = hanshu("胡歌", 18, "演员");
        alert(jieguo);
    </script>
</head>

<body>

</body>

</html>

封装函数

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript</title>
    <script>
        function hanshu(c){
            console.log(c.name + c.age + "岁了," + "是一个" + c.shenfen);
        }
        var duixiang = {
            name:"胡歌",
            age:20,
            shenfen: "演员"
        };
        function fun(a){
            a(duixiang);
        }
        fun(hanshu);
    </script>
</head>

<body>

</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值