当前位置:网站首页>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 .
边栏推荐
- Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
- 实例讲解将Graph Explorer搬上JupyterLab
- JVM memory model
- AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
- PowerDesigner design name and comment replacement
- How can a programmer grow rapidly
- Win11如何取消任务栏隐藏?Win11取消任务栏隐藏的方法
- Simplified pinduoduo product data
- 科技T3国产平台!成功搭载“翼辉国产实时系统SylixOS”
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
猜你喜欢

Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?

Use of common built-in classes of JS

今日群里分享的面试题

Object creation

简单但现代的服务器仪表板Dashdot
![[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached](/img/c8/a205ddc2835c87efa38808cf31f59e.jpg)
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached

GC garbage collection

HLS4ML报错The board_part definition was not found for tul.com.tw:pynq-z2:part0:1.0.

Class loading mechanism
![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
随机推荐
自定义插入页面标签以及实现类似通讯录的首字母搜索
Solve the problem of slow or failed vscode download
一个悄然崛起的国产软件,低调又强大!
[research materials] national second-hand housing market monthly report January 2022 - Download attached
2022/6/8-2022/6/12
P2433 【深基1-2】小学数学 N 合一
Image acquisition and playback of coaxpress high speed camera based on pxie interface
switch 有四样写法你知道么
The key to the success of digital transformation enterprises is to create value with data
面试题篇一
Is Dao safe? Build finance encountered a malicious governance takeover and was looted!
Unreal Engine packaging project
[exercise] HashSet
PowerDesigner design name and comment replacement
关于元宇宙下一代入口——脑机接口的实现
振弦采集模块测量振弦传感器的流程步骤
MySQL signale une erreur can 't create table' demo01. TB Étudiant '(errno: 150)
SQL getting started plan-1-select
MYSLQ十种锁,一篇文章带你全解析