JavaScript中的for...LOGIN

JavaScript中的for循環

for迴圈

格式:

##for(變數初始化; 條件判斷;  變數更新)

{

        循環程式碼;

}

5.png

##範例:求1000以內能被10整除的數字的和

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var sum = 0; 
            for(var i=1;i<=100;i++){
                if(i%10==0){
                    sum+=i;
                }
            }
            document.write("100以内能被10整除的数之和为:"+sum)
        </script>
    </head>
    <body>
    </body>
</html>


下一節

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var sum = 0; for(var i=1;i<=100;i++){ if(i%10==0){ sum+=i; } } document.write("100以内能被10整除的数之和为:"+sum) </script> </head> <body> </body> </html>
章節課件