javascript输出菱形
时间: 2023-12-15 10:07:08 浏览: 73
js实现菱形的输出
5星 · 资源好评率100%
以下是JavaScript输出菱形的代码:
```javascript
function printDiamond(n) {
for (let i = 1; i <= n; i++) {
let row = "";
for (let j = 1; j <= n - i; j++) {
row += " ";
}
for (let k = 1; k <= i * 2 - 1; k++) {
row += "*";
}
console.log(row);
}
for (let i = n - 1; i >= 1; i--) {
let row = "";
for (let j = 1; j <= n - i; j++) {
row += " ";
}
for (let k = 1; k <= i * 2 - 1; k++) {
row += "*";
}
console.log(row);
}
}
printDiamond(5);
```
输出结果为:
```
*
***
*****
*******
*********
*******
*****
***
*
```
阅读全文