当前位置:网站首页>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 .
边栏推荐
- Oracle物理体系结构
- After studying 11 kinds of real-time chat software, I found that they all have these functions
- Cookie和Session的相关概念
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- 毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
- Test self-study people must see: how to find test items in software testing?
- Win11怎么关闭开机自启动软件
- 类加载机制
- Interesting! Database is also serverless!
- servlet知识点
猜你喜欢

简单但现代的服务器仪表板Dashdot

Win11暂停更新点不了怎么办?Win11暂停更新是灰色的如何解决?
![[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
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

GC垃圾回收
![[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

MYSLQ十种锁,一篇文章带你全解析

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

Hls4ml reports an error the board_ part definition was not found for tul. com. tw:pynq-z2:part0:1.0.

Interview questions shared in today's group
随机推荐
How to correctly use vertx to operate redis (3.9.4 with source code analysis)
STC 32位8051单片机开发实例教程 三 程序编译设置与下载
【let var const】
Regular expression =regex=regular expression
New window open page -window open
Uni app product classification
DS Transunet:用于医学图像分割的双Swin-Transformer U-Net
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
servlet知识点
P2433 [deep foundation 1-2] primary school mathematics n in one
Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?
产品模块化设计的前世今生
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
The key to the success of digital transformation enterprises is to create value with data
[exercise] HashSet
A quietly rising domestic software, low-key and powerful!
Win11怎么关闭开机自启动软件
HLS4ML报错The board_part definition was not found for tul.com.tw:pynq-z2:part0:1.0.
自定义插入页面标签以及实现类似通讯录的首字母搜索
Servlet knowledge points