JavaScript循环案例

本文展示了JavaScript中使用for循环和while循环解决多种编程问题的例子,包括打印7的倍数、求100以内偶数和、计算100的阶乘、找水仙花数、列出闰年以及构建99乘法表。这些实例涵盖了基础的数学计算、条件判断及输出格式化,是JavaScript初学者的宝贵资源。
摘要由CSDN通过智能技术生成

JavaScript循环案例

1.打印100以内7的倍数

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        for (var i = 1; i < 100; i++) {
            if (i % 7 == 0)
                console.log(i);
        }
    </script>
</body>

</html>

2.打印100以内所有偶数的和

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var sum = 0;
        for (var i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                sum += i;
            }
        }
        console.log(sum);
    </script>
</body>

</html>

3.计算100的阶乘100!

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var res = 1;
        for (var i = 100; i >= 1; i--) {
            res *= i;
        }
        console.log(res);
    </script>
</body>

</html>

4.100-999之间的水仙花数,abc=a3+b3+c3

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        for (var i = 100; i <= 999; i++) {
            var n1 = parseInt(i / 100);
            var n2 = parseInt(i % 100 / 10); //632==>32==>3.2==>3
            var n3 = i % 10;
            if (i == n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3) {
                console.log(i);
            }
        }
    </script>
</body>

</html>

5.打印出1000-2000之间所有的闰年数,并以每行4个数的形式输出

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var i = 0;
        for (var a = 1000; a <= 2000; a++) {
            if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) {
                document.write(a + '&nbsp' + '');
                i++;
                if (i % 4 == 0) {
                    document.write("<br>");
                }
            }
        }
    </script>
</body>

</html>

6.打印99乘法表

for循环:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        span {
            border: 1px solid #8eff4f;
        }
    </style>
</head>

<body>

</body>
<script>
    //函数的定义
    function show99() {
        //ul和li
        document.write("<ul>")
        for (let i = 1; i <= 9; i++) {
            document.write("<li>")
            for (let j = 1; j <= i; j++) {
                document.write("<span>" + i + "*" + j + "=" + i * j + "&emsp;</span>")
            }
            document.write("</li>")

        }
        document.write("</ul>")
    }
    show99(); //函数的调用
</script>

</html>

while循环:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html {
            font-family: monospace;
        }
    </style>
    <script>
        var i = 0;
        do {
            i++;
            var j = 0;
            do {
                j++;
                document.write(i + "*" + j + "=" + i * j, "&nbsp;")
            }
            while (j < i);
            document.write("<br>");
        }
        while (i < 9);
    </script>
</head>

<body>

</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值