In JavaScript, you can use loops and conditional statements to print pyramids. This article will introduce several ways to implement pyramids, including using for loops, while loops, and recursive functions.
The pyramid is composed of rows of asterisks. First, you need to determine the height of the pyramid, and then use a for loop to print the height of each row. Asterisk.
The following is the code for printing the pyramid using a for loop:
function printPyramid(height) { for (var i = 1; i <= height; i++) { var row = ''; for (var j = 1; j <= height - i; j++) { row += ' '; // 添加空格 } for (var k = 1; k <= 2 * i - 1; k++) { row += '*'; // 添加星号 } console.log(row); // 打印每一行 } }
In the above code, the first for loop is used to control the number of lines printed, and the second for loop is used to print Leading spaces, the third for loop is used to print asterisks. In each row, the number of asterisks is 2i-1, where i is the row number of the current row.
You can use a while loop to print the pyramid, replace the counter in the for loop with a variable, and increment that each time through the loop The value of the variable.
The following is the code for using a while loop to print the pyramid:
function printPyramid(height) { var i = 1; while (i <= height) { var row = ''; var j = 1; while (j <= height - i) { row += ' '; // 添加空格 j++; } var k = 1; while (k <= 2 * i - 1) { row += '*'; // 添加星号 k++; } console.log(row); // 打印每一行 i++; } }
This code is very similar to the code using a for loop, except that the counter is replaced by a variable, and then a while loop is used to control the number of loops .
A recursive function is a method of breaking a problem into smaller sub-problems and calling itself repeatedly to solve the problem. You can use a recursive function to print the pyramid. The parameter of the recursive function is the height of the pyramid.
The following is the code to print the pyramid using a recursive function:
function printPyramidRecursively(height) { if (height > 0) { printPyramidRecursively(height - 1); var row = ''; for (var i = 0; i < height; i++) { row += '* '; } console.log(row); } }
In the above code, the termination condition of the recursive function is that the height of the pyramid is zero. In each recursive call, the previous part is output first, and then a line consisting of asterisks is printed. The final output is a complete pyramid.
Summary
Using JavaScript, it is possible to print pyramids through loops and recursive functions. No matter which method you use, you need to determine the height of the pyramid and use a loop or recursive function to print the asterisks for each row based on the height. As the height increases, the size of the pyramid increases, so you need to make sure you are using a computer or browser powerful enough to handle larger pyramids.
The above is the detailed content of How to print pyramid using javascript. For more information, please follow other related articles on the PHP Chinese website!