当前位置:网站首页>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 .
边栏推荐
- 开发那些事儿:EasyCVR平台添加播放地址鉴权功能
- A quietly rising domestic software, low-key and powerful!
- Anaconda installs the virtual environment to the specified path
- Example explanation: move graph explorer to jupyterlab
- 1592 example 1 King (sgu223 loj10170 luogu1896 increase + / provincial election -) violent thinking pressure DP 01 Backpack
- AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
- Cookie和Session的相关概念
- Use of common built-in classes of JS
- JS proxy
- February 15, 2022: sweeping robot. There is a floor sweeping robot in the room (represented by a grid). Each grid in the grid has two possibilities: empty and obstacles. The sweeping robot provides fo
猜你喜欢
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
JS的Proxy
为定时器和延时器等其它情况的回调函数绑定当前作用域的this
使用Zadig从0到1搭建持续交付平台
Hls4ml reports an error the board_ part definition was not found for tul. com. tw:pynq-z2:part0:1.0.
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
开发那些事儿:EasyCVR集群设备管理页面功能展示优化
EasyCVR集群视频广场页面切换时,请求流未能终止的问题优化
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Is Dao safe? Build finance encountered a malicious governance takeover and was looted!
随机推荐
2022/5/23-2022/5/30
HLS4ML进入方法
JS proxy
Wechat applet realizes keyword highlighting
Linux下安装Redis,并配置环境
[research materials] Huawei Technology ICT 2021: at the beginning of the "Yuan" year, the industry is "new" -- download attached
Interview questions shared in today's group
对象的创建
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
windows环境 redis安装和启动(后台启动)
After studying 11 kinds of real-time chat software, I found that they all have these functions
使用 Kibana Timelion 进行时间序列分析
Win11如何取消任务栏隐藏?Win11取消任务栏隐藏的方法
Difference between redo and undo
STC 32位8051单片机开发实例教程 三 程序编译设置与下载
安装sharp报错
Anaconda安装虚拟环境到指定路径
Solve the problem of slow or failed vscode download
Leetcode 1380 lucky numbers in matrix [array] the leetcode path of heroding
再回顾集合容器