当前位置:网站首页>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 .
边栏推荐
猜你喜欢

振弦采集模塊測量振弦傳感器的流程步驟

Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?

Optaplanner learning notes (I) case cloud balance

安装sharp报错

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results

Win11快捷键切换输入法无反应怎么办?快捷键切换输入法没有反应

【AI服务器搭建】CUDA环境

通过js实现金字塔(星号金字塔,回文对称数字金字塔)

STC 32位8051单片机开发实例教程 三 程序编译设置与下载

What is the essential difference between Bi development and report development?
随机推荐
3D全景模型展示可视化技术演示
DS Transunet:用于医学图像分割的双Swin-Transformer U-Net
简单但现代的服务器仪表板Dashdot
SQL 入门计划-1-选择
Basic use of MySQL
Transaction isolation level gap lock deadlock
[research materials] national second-hand housing market monthly report January 2022 - Download attached
Interview question 1
The key to the success of digital transformation enterprises is to create value with data
Win11快捷键切换输入法无反应怎么办?快捷键切换输入法没有反应
How to use console Log print text?
How to add transactions in JDBC
Unreal Engine packaging project
JS ternary expression complex condition judgment
利用win7漏洞进行系统登录密码破解
振弦采集模塊測量振弦傳感器的流程步驟
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Linux下安装Redis,并配置环境
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
STC 32位8051单片机开发实例教程 三 程序编译设置与下载