当前位置:网站首页>js在控制台输出菱形
js在控制台输出菱形
2022-07-26 09:18:00 【小萌新入场】
js在控制台输出菱形
以一个上半部分10行,下半部分9行的为例
var str = ""; //在控制台输出要采用字符串拼接,所以先定义一个空字符串
for (var row = 1; row <= 10; row++) { //外层循环控制行数,先输出上半部分的10行
for (var j = 1; j <= 10 - row; j++) { //因为第一行有9个空格, 1个星星。为了更直白,用@替代空格
str = str + "@" //因为第二行有8个空格, 3个星星
} //因为第三行有7个空格, 5个星星
//因为第四行有6个空格, 7个星星
// ...........
//因为第十行有0个空格,19个星星
//所以空格和行数之间的关系就是:j<=(10-row)
for (var i = 1; i <= 2 * row - 1; i++) { //星星和行数之间的关系就是:(2*row-1)
str = str + "*";
}
str = str + "\n"; //外层循环,每输出一行要换行
}
//下半部分和上半部分大致相同
for (var row = 1; row <= 9; row++) { //外层循环控制行数,输出下半部分的9行
for (var j = 1; j <= row; j++) { //因为第一行有1个空格,17个星星
str = str + "@" //因为第二行有1个空格,15个星星
} //因为第三行有1个空格,13个星星
//因为第四行有1个空格,11个星星
//...............
//因为第一行有9个空格, 1个星星
//所以空格和行数之间的关系就是:j<=row
for (var i = 1; i <= 19 - 2 * row; i++) { //星星和行数之间的关系就是总的行数19减去2倍的row即:19-2*row
str = str + "*";
}
str = str + "\n"; //外层循环,每输出一行要换行
}
console.log(str);

我直接照搬过来了,哈哈! 供大家参考哈!
看到既是,好运连连!
————————————————
版权声明:本文为CSDN博主「Fire1122」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Fire1122/article/details/108308918
边栏推荐
- 力扣——二叉树剪枝
- 2022化工自动化控制仪表操作证考试题模拟考试平台操作
- PAT 甲级 A1034 Head of a Gang
- redis原理和使用-安装和分布式配置
- Matlab 绘制阴影误差图
- (2006,Mysql Server has gone away)问题处理
- Pat grade a A1034 head of a gang
- Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
- "No input file specified" problem handling
- Simple message mechanism of unity
猜你喜欢

Horizontal comparison of the data of the top ten blue chip NFTs in the past half year

Introduction to excellent verilog/fpga open source project (30) - brute force MD5

ext3文件系统的一个目录下,无法创建子文件夹,但可以创建文件

李沐d2l(四)---Softmax回归

redis原理和使用-基本特性

Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain

The essence of attack and defense strategy behind the noun of network security

Study notes of canal

What is the difference between NFT and digital collections?

Li Mu D2L (VI) -- model selection
随机推荐
Pat grade a A1034 head of a gang
Error: Cannot find module ‘umi‘ 问题处理
839. Simulation reactor
滑动窗口、双指针、单调队列、单调栈
Tornado multi process service
Flask project learning (I) -- sayhello
"No input file specified" problem handling
Li Mu D2L (V) -- multilayer perceptron
"Could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32"
The Child and Binary Tree-多项式开根求逆
JVM触发minor gc的条件
Unity topdown character movement control
高数 | 武爷『经典系列』每日一题思路及易错点总结
Cat installation and use
2B和2C
Thread Join 和Object wait 的区别
对标注文件夹进行清洗
Clean the label folder
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
The child and binary tree- open root inversion of polynomials