JavaScript loop printing case

JavaScript loop printing case

1. Print 5 rows and 5 columns *

  • run code

	var str = '';
		for (var i = 1; i <= 5; i++) {
    
    //外部控制换行
			for (var j = 1; j <= 5; j++) {
    
    //内部负责控制打印
				str = str + '*';
			}
			// 内部执行结束换行
			str = str + '\n';
		}
		console.log(str);//输出打印结果
  • operation result

Insert image description here

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);//输出打印结果
  • operation result

Insert image description here

3. Print 10 lines of inverted triangles

  • run code

var str = '';
		for (var i = 1; i <= 10; i++) {
    
    //外部控制换行
			for (var j = i; j <= 10; j++) {
    
    //内部负责控制打印
				str = str + '*';
			}
			// 内部执行结束换行
			str = str + '\n';
		}
		console.log(str);//输出打印结果
  • operation result

Insert image description here

4. Print the multiplication table

Brief description of the case

Insert image description here

  • run code

	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

Je suppose que tu aimes

Origine blog.csdn.net/SDXYGZH/article/details/126297243
conseillé
Classement