JavaScript loop printing case
1. Print 5 rows and 5 columns *
var str = '';
for (var i = 1; i <= 5; i++) {
for (var j = 1; j <= 5; j++) {
str = str + '*';
}
str = str + '\n';
}
console.log(str);
![Insert image description here](https://img-blog.csdnimg.cn/a03470b45a5a476e88a4be2c657893f1.png)
2. Print n rows and m columns *
-
Enter "Number of lines to print" in the console
var n = prompt('请输入打印的行数')
var m = prompt('请输入打印的列数')
var str = '';
for (var i = 1; i <= n; i++) {
for (var j = 1; j <= m; j++) {
str = str + '*';
}
str = str + '\n';
}
console.log(str);
![Insert image description here](https://img-blog.csdnimg.cn/f4300c7472bd4e01831f9830db92ef36.png)
3. Print 10 lines of inverted triangles
var str = '';
for (var i = 1; i <= 10; i++) {
for (var j = i; j <= 10; j++) {
str = str + '*';
}
str = str + '\n';
}
console.log(str);
![Insert image description here](https://img-blog.csdnimg.cn/f85667ecdcfc4f9d9b1ac80fd7e61c33.png)
4. Print the multiplication table
Brief description of the case
![Insert image description here](https://img-blog.csdnimg.cn/ccffd5efd5ee49289abfc22b1e0ba80d.png)
var str = '';
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
str = str + i + '*' + j + '=' + i * j + '\t';
}
str = str + '\n';
}
console.log(str);
-
operation result
![Insert image description here](https://img-blog.csdnimg.cn/cd94d2e804624344b339d61486adc939.png)