我是靠谱客的博主 朴素钥匙,这篇文章主要介绍JS学习-for循环,嵌套,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> /* for(var i=0; i<=100; ++i){ document.write("<br/>"+i) } for(exp1; exp2; exp3){ 循环体; } exp1 无条件执行的第一个表达式 exp2 判断是否执行循环条件 exp3 做增量操作 */ //++自增运算符 --自减 /* document.write('<hr color="red"/>') for(i=1; i<=100; i+=2){ document.write("<br />"+i) } document.write("<hr color='red'/>") for(i=1; i<=100; ++i){ if(i%2==1){ document.write(i+"<br />") } } */ document.write("<hr color='green'/>") //var a=1; //alert(++a); var sum=0; for(i=1; i<=100; ++i){ sum+=i; } document.write(sum); document.write("<hr color='orange'/>") //100以内奇数的和 var sumj=0; for(i=1; i<=100; i+=2){ sumj+=i; } document.write(sumj); document.write("<hr color='greenyellow'/>"); //for的内嵌循环嵌套 for(var i=1; i<=3; i++){ document.write('外层循环的第'+i+'次循环<br/>'); for(var j=1; j<=4; j++){ document.write('内层循环的第'+j+'次循环<br/>'); } document.write('<hr color="mediumvioletred"/>') } //for的嵌套:外层循环一次然后内部循环全部,外部再依次循环。 </script> //for循环制作表格 <table border='1' cellpadding='0' cellpadding='0' bgcolor="aqua" width="80%"> <script type="text/javascript"> for(var i=1; i<=3; i++){ document.write('<tr>') for(var j=1; j<=3; j++){ document.write('<td>x</td>') } document.write('</tr>') } </script> </table> <hr color='rdarkmagenta'/> <table border="1" cellpadding="0" cellspacing="0" bgcolor="antiquewhite" width="80%"> <script type="text/javascript"> for(i=1; i<=9; i++){ document.write('<tr>'); for(j=1; j<=i; j++){ document.write('<td>'+j+"*"+i+"="+(i*j)+'</td>'); } document.write('</tr>'); } </script> </table> <!--外层循环为行,内层循环为列--> <hr color="red"/> <!-- 百钱买百鸡 母鸡3元1只,小鸡3只1元,100元能买多少? --> <script type="text/javascript"> for(i=1; i<=100/3; i++){ document.write("母鸡数量为"+i+"只,小鸡数量为"+(100-3*i)*3+"只。<br/>"); } </script> <!-- <table border="1" cellpadding="0" cellspacing="0" bgcolor="orange" width="80%"> <script type="text/javascript"> for(r=1; r<=100/3; r++){ document.write('<tr>') for(i=1; i<=100/3; i++){ document.write("<td>"+"母鸡数量为"+i+"只,小鸡数量为"+(100-3*i)*3+"只。<br/>"+"</td>"); } document.write('</tr>') } </script> </table> 嵌套还是有些问题,会产生33行相同的内容,为什么???? --> </body> </html>

 


5050


2500


外层循环的第1次循环
内层循环的第1次循环
内层循环的第2次循环
内层循环的第3次循环
内层循环的第4次循环


外层循环的第2次循环
内层循环的第1次循环
内层循环的第2次循环
内层循环的第3次循环
内层循环的第4次循环


外层循环的第3次循环
内层循环的第1次循环
内层循环的第2次循环
内层循环的第3次循环
内层循环的第4次循环


//for循环制作表格

xxx
xxx
xxx

1*1=1
1*2=22*2=4
1*3=32*3=63*3=9
1*4=42*4=83*4=124*4=16
1*5=52*5=103*5=154*5=205*5=25
1*6=62*6=123*6=184*6=245*6=306*6=36
1*7=72*7=143*7=214*7=285*7=356*7=427*7=49
1*8=82*8=163*8=244*8=325*8=406*8=487*8=568*8=64
1*9=92*9=183*9=274*9=365*9=456*9=547*9=638*9=729*9=81

母鸡数量为1只,小鸡数量为291只。
母鸡数量为2只,小鸡数量为282只。
母鸡数量为3只,小鸡数量为273只。
母鸡数量为4只,小鸡数量为264只。
母鸡数量为5只,小鸡数量为255只。
母鸡数量为6只,小鸡数量为246只。
母鸡数量为7只,小鸡数量为237只。
母鸡数量为8只,小鸡数量为228只。
母鸡数量为9只,小鸡数量为219只。
母鸡数量为10只,小鸡数量为210只。
母鸡数量为11只,小鸡数量为201只。
母鸡数量为12只,小鸡数量为192只。
母鸡数量为13只,小鸡数量为183只。
母鸡数量为14只,小鸡数量为174只。
母鸡数量为15只,小鸡数量为165只。
母鸡数量为16只,小鸡数量为156只。
母鸡数量为17只,小鸡数量为147只。
母鸡数量为18只,小鸡数量为138只。
母鸡数量为19只,小鸡数量为129只。
母鸡数量为20只,小鸡数量为120只。
母鸡数量为21只,小鸡数量为111只。
母鸡数量为22只,小鸡数量为102只。
母鸡数量为23只,小鸡数量为93只。
母鸡数量为24只,小鸡数量为84只。
母鸡数量为25只,小鸡数量为75只。
母鸡数量为26只,小鸡数量为66只。
母鸡数量为27只,小鸡数量为57只。
母鸡数量为28只,小鸡数量为48只。
母鸡数量为29只,小鸡数量为39只。
母鸡数量为30只,小鸡数量为30只。
母鸡数量为31只,小鸡数量为21只。
母鸡数量为32只,小鸡数量为12只。
母鸡数量为33只,小鸡数量为3只。

最后

以上就是朴素钥匙最近收集整理的关于JS学习-for循环,嵌套的全部内容,更多相关JS学习-for循环内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部