17.JavaScript-循环嵌套练习
1. 循环嵌套规律
在循环嵌套中外循环控制的是行数,内循环控制的是列数
-----------------------------------------------------1---------------------------------------------------------
需求:输出3行 ****
for (let j = 0; j < 1; j++) {
for (let i = 0; i < 4; i++) {
document.write("*");
}
document.write("<br>");
}
-----------------------------------------------------2---------------------------------------------------------
需求:在界面中输出如下图形(尖尖朝下)
*****
****
***
**
*
for (let i = 0; i < 5; i++) {
for (let j = i; j < 5; j++) {
document.write("*");
}
document.write("<br>");
}
-----------------------------------------------------3---------------------------------------------------------
需求:在界面中输出如下图形(尖尖朝上)
*
**
***
****
*****
for (let i = 0; i < 5; i++) {
for (let j = 0; j <= i; j++) {
document.write("*");
}
document.write("<br>");
}
-----------------------------------------------------4---------------------------------------------------------
需求:在界面中输出如下图形
1
12
123
for (let i = 0; i < 3; i++) {
for (let j = 0; j <=i; j++) {
document.write(j+1);
}
document.write("<br>");
}
-----------------------------------------------------5---------------------------------------------------------
需求:在界面中输出如下图形
1
22
333
for (let i = 0; i < 3; i++) {
for (let j = 0; j <=i; j++) {
document.write(i+1);
}
document.write("<br>");
}
-----------------------------------------------------6---------------------------------------------------------
需求:输出正三角形
-----*
----***
---*****
--*******
-*********
***********
for (let i = 0; i < 5; i++) {
for (let j = i; j <5; j++) {
document.write("-");
}
for(let m = 0;m<2*i+1;m++){
document.write("*");
}
document.write("<br>");
}
-----------------------------------------------------7---------------------------------------------------------
需求:输出九九乘法表
for (let i = 1; i <= 9; i++) {
for (let j = 1; j <=i; j++) {
document.write(j + "*" + i + "=" + i*j);
}
document.write("<br>");
}