当前位置:网站首页>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 .
边栏推荐
- 安装sharp报错
- ModSim基本使用(Modbus模拟器)
- The large list set is divided into multiple small list sets in equal proportion
- torch. nn. functional. Interpolate function
- A quietly rising domestic software, low-key and powerful!
- SwiftUI 4 新功能大全之 Toggle与 Mixed Toggle 多个绑定组件
- Uni app wechat applet one click login to obtain permission function
- 【多线程】锁策略
- switch 有四样写法你知道么
- 一个悄然崛起的国产软件,低调又强大!
猜你喜欢
How to use console Log print text?
通过js实现金字塔(星号金字塔,回文对称数字金字塔)
Cookie和Session的相关概念
全国职业院校技能大赛网络安全“splunk“详细配置
Win11如何取消任务栏隐藏?Win11取消任务栏隐藏的方法
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
【无标题】
Modsim basic use (Modbus simulator)
Related concepts of cookies and sessions
随机推荐
Stack Overflow 2022 开发者调查:行业走向何方?
一文读懂C语言中的结构体
Source code series of authentic children -inheritablethreadlocal (line by line source code takes you to analyze the author's ideas)
Interview questions shared in today's group
The key to the success of digital transformation enterprises is to create value with data
【let var const】
DS Transunet:用于医学图像分割的双Swin-Transformer U-Net
How to correctly use vertx to operate redis (3.9.4 with source code analysis)
[exercise] HashSet
Unreal Engine packaging project
全国职业院校技能大赛网络安全“splunk“详细配置
Win11快捷键切换输入法无反应怎么办?快捷键切换输入法没有反应
【多线程】锁策略
Collect Tiktok video
GC垃圾回收
Mysql查询结果去除换行
Hls4ml entry method
Wechat applet realizes keyword highlighting
Redis installation and startup in Windows environment (background startup)
JDBC中如何添加事务