当前位置:网站首页>Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
2022-07-01 19:54:00 【igloo 】
Take a look at the implementation effect first :
Every time I see something like this that needs to implement graphics , The first step is to find the law of graphics .
Start with the law !
Seeing and deciding this figure requires spaces and ’'.
From the top down , The number of stars is :1,3,5,7,9,11…
The number of spaces is :9 8 7 6 5 4 3 2 1 0
Then the law of stars is 2n-1 , Space is decreasing every layer 1
But through js To achieve , You need to start from the array ( The pyramids ) Index and length of the angle to write their rules ,
Then the stars are : 2( Index value +1)-1 ,
And the space is : Height - Index value .
After knowing the rules , Add a string :str.repeat(n) We can realize the digital pyramid ~
The code is as follows :
function tar(n){
for(let i=0;i<n;i++){
let str1=' '
let str2 = str1.repeat(n-i)
let str3='*'
let str4 = str3.repeat(2*(i+1)-1)
console.log(str2+str4)
}
}
call :
tar(10)
Achieve results :
In fact, the code is very simple , Don't understand why others don't str.repeat, So easy hhhhh
Advanced : Make a pyramid of palindrome numbers . Right and left symmetry
Let's look at the implementation results first :
ha-ha , In fact, the soup is not changed Or continue to find rules
n=0 yes 1 n=1 yes 121 n=2 yes 12321
Since it's palindrome , Just write one side , On the other side, through the array method (reverse) That's all right. , Convert to string splicing .
So the simplification is
n=0 yes 1 n=1 yes 12 n=2 yes 123
Implementation code :
// Realize palindrome number
function countNum(n){
let arr=[]
for(let i=0;i<n;i++){
arr.push(i+1)
}
let str = arr.join("")
let arr1 = str.split("").reverse()
arr1.shift()
let str1 = arr1.join("")
return str+str1
}
// Main code
function tar(n){
for(let i=0;i<n;i++){
let str1=' '
let str2 = str1.repeat(n-i)
let str3 = countNum(i+1)
console.log(str2+str3)
}
}
call :
tar(9)
The implementation result is shown in the figure above , Successful implementation ~ Very simple, actually . I didn't read the code written by others , It's not the optimal solution , But this can help a front end be more proficient in using strings , Array method , And the flexible application of the conversion between the two , Strengthen the memory .
边栏推荐
- DS transunet: Dual Swing transformer u-net for medical image segmentation
- SQL 入门计划-1-选择
- Botu V16 obtains the system time and converts it into a string
- 为定时器和延时器等其它情况的回调函数绑定当前作用域的this
- tensorflow报错Could not load dynamic library ‘libcudnn.so.8
- qobject_cast用法
- Interview questions shared in today's group
- Simplified pinduoduo product data
- Wireshark packet analysis TCP, FTP
- Understand the structure in C language in one article
猜你喜欢

Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road

Solve the problem of slow or failed vscode download

使用Zadig从0到1搭建持续交付平台

【AI服务器搭建】CUDA环境

Image acquisition and playback of coaxpress high speed camera based on pxie interface
![Leetcode 1380 lucky numbers in matrix [array] the leetcode path of heroding](/img/56/0c7f39406814b5bf92eca5f6670e10.jpg)
Leetcode 1380 lucky numbers in matrix [array] the leetcode path of heroding

STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置

JVM内存模型

解决VSCode下载慢或下载失败的问题

Test self-study people must see: how to find test items in software testing?
随机推荐
PowerDesigner设计Name和Comment 替换
Wireshark packet analysis TCP, FTP
MySQL signale une erreur can 't create table' demo01. TB Étudiant '(errno: 150)
1592 例题1 国王(Sgu223 LOJ10170 LUOGU1896 提高+/省选-) 暴力思考 状压DP 01背包
Install redis under Linux and configure the environment
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
Time series analysis using kibana timelion
Mysql查询结果去除换行
2022/6/8-2022/6/12
Simplified pinduoduo product data
Wechat applet realizes keyword highlighting
How to add transactions in JDBC
【无标题】
一个悄然崛起的国产软件,低调又强大!
自定义插入页面标签以及实现类似通讯录的首字母搜索
qobject_ Cast usage
switch 有四样写法你知道么
Linux下安装Redis,并配置环境