码迷,mamicode.com
首页 > Web开发 > 详细

逆战班!js循环案例

时间:2020-04-03 01:05:19      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:循环   col   tle   str   oct   cal   view   html   write   

##打印乘法表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        for( var j =1 ; j<10; j++){
            for( var i = 1; i <=j; i++){
                document.write(j,‘X‘,i,‘=‘,j*i,‘&nbsp&nbsp‘)
            }
            document.write(‘<br>‘)
        }
    </script>
</body>
</html>

打印结果:

1X1=1  
2X1=2  2X2=4  
3X1=3  3X2=6  3X3=9  
4X1=4  4X2=8  4X3=12  4X4=16  
5X1=5  5X2=10  5X3=15  5X4=20  5X5=25  
6X1=6  6X2=12  6X3=18  6X4=24  6X5=30  6X6=36  
7X1=7  7X2=14  7X3=21  7X4=28  7X5=35  7X6=42  7X7=49  
8X1=8  8X2=16  8X3=24  8X4=32  8X5=40  8X6=48  8X7=56  8X8=64  
9X1=9  9X2=18  9X3=27  9X4=36  9X5=45  9X6=54  9X7=63  9X8=72  9X9=81

 

##打印*号金字塔

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var a = prompt()
        for(var b=0;b<=a;b++){
            for(var c=0;c<=b;c++){
                document.write(‘*‘)
            }
        document.write(‘<br>‘)
        }
    </script>
</body>
</html>

打印结果:

*
**
***
****
*****
******
##阶乘累加

1!+2!+3!+...+n!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var res = 0
        for(var j=1;j<=3;j++){
            var a = 1
            for(var i=1;i<=j;i++){
                a*=i 
            } 
        res += a
        }
        console.log(res)
    </script>
</body>
</html>

##阿基米德与国王的故事

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var res = 1
        var sum = 1
        for(var i=1;i<64;i++){
            res = res*2
            sum = sum += res
        }
        // console.log((sum/100000).toFixed(5))
        console.log(sum)
    </script>
</body>
</html>

结果:18446744073709552000 (国王输惨了)

逆战班!js循环案例

标签:循环   col   tle   str   oct   cal   view   html   write   

原文地址:https://www.cnblogs.com/lanbai/p/12623926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!