美文网首页
JS循环练习

JS循环练习

作者: 蔚来天空 | 来源:发表于2018-09-18 07:39 被阅读0次

    1,打印1-100之间所有奇数之和

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>打印1-100之间所有奇数之和</title>
        <script type="text/javascript">
            var sum=0;
            for (var i = 1; i <=100; i++) {
                
                if (i % 2 != 0) {
                    sum += i    
                }
            }
            alert(sum);
        </script>
    </head>
    <body>
        
    </body>
    </html>
    

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

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>1-100之间所有7的倍数个数及和</title>
        <script type="text/javascript">
            var list = [];
            var sum = 0;
            for (var i = 1; i <= 100; i++) {
                if (i % 7 == 0) {
                    list.push(i)
                    sum+=i
                }
            }
            alert(list.length + 1);
            alert(sum);
        </script>
    </head>
    <body>
        
    </body>
    </html>
    

    3.水仙花数

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>水仙花数</title>
        <script type="text/javascript">
            var gw,sw,bw;
            for (var i = 100; i <= 999; i++) {
                bw = parseInt(i / 100);
                sw = parseInt((i / 10) % 10);
                gw = parseInt(i % 10);
                if (i == bw*bw*bw + sw*sw*sw + gw*gw*gw) {
                    document.write(i + '是水仙花数' + '<br />');
                }
            }   
        </script>
    </head>
    <body>
        
    </body>
    </html>
    

    4.判断是否为质数

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>判断是否为质数</title>
        <script type="text/javascript">
            if(num<=1||isNaN(num)){
                alert('请正确输入!')
                }else{
                    var flag = true;
                    for(var i = 2;i<num;i++){
                        if(num % i ==0){
                            flag = false;
                        }
                    }
                    if(flag){
                        alert('是质数');
                    }         
                }
    
        </script>
    </head>
    <body>
        
    </body>
    </html>
    

    5.九九乘法表

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>九九乘法表</title>
        <script type="text/javascript">
            for (var n = 1; n <= 9; n++) {
                for (var m = 1;m <=9; m++) {
                    var b = m*n;
                    document.write('' + m + '*' + n + '=' + b + ' ');
                }   
                document.write('<br/>');        
            }
            
        </script>
    </head>
    <body>
        
    </body>
    </html>
    

    while循环

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>while循环_</title>
        <script type="text/javascript">
            // 1、假如投资的年利率为5%,试求从1000块增长到5000块,需要花费多少年
           // 1000 1000+10000.05=10001.05
          // 1050 1050+10500.05=10501.05
         // ……
        // 5000
            var money = 1000;
                var count = 0;
    
            while(money <= 5000){
                money*=1.05;
                count++;
            }
            console.log(count)
        </script>
    </head>
    <body>
        
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:JS循环练习

          本文链接:https://www.haomeiwen.com/subject/pibenftx.html