当前位置:网站首页>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 .
边栏推荐
- How can a programmer grow rapidly
- list分割成满足和不满足条件的集合(partitioningBy)
- Process steps of vibrating wire acquisition module for measuring vibrating wire sensor
- [research data] observation on the differences of health preservation concepts among people in 2022 - Download attached
- [AI server setup] CUDA environment
- New window open page -window open
- 开发那些事儿:EasyCVR集群设备管理页面功能展示优化
- JS ternary expression complex condition judgment
- 科技T3国产平台!成功搭载“翼辉国产实时系统SylixOS”
- Optaplanner learning notes (I) case cloud balance
猜你喜欢
![[AI server setup] CUDA environment](/img/ca/2acfc42ea3ba24934a89472a8632f6.png)
[AI server setup] CUDA environment

Redis installation and startup in Windows environment (background startup)

Hls4ml entry method
![[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached](/img/50/926cc5bce83f8b195b3e2072b656bf.jpg)
[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached

Procédure de mesure du capteur d'accord vibrant par le module d'acquisition d'accord vibrant

Object creation

Detailed configuration of network security "Splunk" in national vocational college skills competition

Solve the problem of slow or failed vscode download

通过js实现金字塔(星号金字塔,回文对称数字金字塔)
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]
随机推荐
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
Uni app product classification
对金额进行求和
Oracle物理体系结构
Modsim basic use (Modbus simulator)
Is Dao safe? Build finance encountered a malicious governance takeover and was looted!
Understand the structure in C language in one article
科技T3国产平台!成功搭载“翼辉国产实时系统SylixOS”
Use of common built-in classes of JS
面试题篇一
mysql 報錯 Can‘t create table ‘demo01.tb_Student‘ (errno: 150)*
How to correctly use vertx to operate redis (3.9.4 with source code analysis)
mysql 报错 Can‘t create table ‘demo01.tb_Student‘ (errno: 150)*
STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置
Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
servlet知识点