Implementation example of JavaScript printing star pyramid function
This article mainly introduces JavaScript to realize the function of printing star pyramid, and combines specific examples to analyze the principle and related implementation techniques of JavaScript for outputting any given number of rows of star pyramid graphics. Friends in need can refer to the following
The example in this article describes how to implement the printing star pyramid function in JavaScript. I share it with you for your reference. The details are as follows:
It is exactly the same for you to write in other languages.
I guess you have seen this question when you were learning C language...
That is to say, print the following ghost stuff:
#When I saw the loop structure, I felt very bored, so I disdained this question and did not pay attention to it. Think about it,
Because if it is written in JavaScript, it can be written like this. It is not even JavaScript, it is just an html:
<html> <head> <meta http-equiv="content-type" content="text/html;charset=gb2312"/> </head> <body> &nbsp;&nbsp;* &nbsp;*** ***** &nbsp;*** &nbsp;&nbsp;* </body> </html>
The reason why it is not used here UTF-8 encoding is because UTF-8's processing of nbsp and * font will cause typesetting confusion, that is, it does not use standard Song fonts, which makes the final result impossible to view.
Anyway, I can copy and paste the above content no matter how many lines you put in the question. However, today, when I have programmed to a certain amount and have some big data concepts, I saw this question again and carefully After thinking about it for a while, if the question maker asked to output a star-shaped pyramid with 200,000 lines of symmetry on the central axis, I would be damned.
So we still need to thoroughly understand how to implement this. Although such programming will not occur in actual programming, it is said that this question will still come up in some boring interviews. At least, latecomers will ask you C language When asked, if you output a star pyramid with 200,000 lines of symmetry on the central axis, you still have to know how to do it. If you can't do this question the first time, you can't do it.
1. Basic Goals
First, an input box pops up, allowing the user to enter an odd number. After all, axial symmetry requires an odd number,
Then for the robustness of the program, it is necessary to judge what the user inputs. If the input is not an odd number, a prompt will pop up and the subsequent program will not be executed. How to judge an odd number in JavaScript? I have already discussed it in "JavaScript's Judgment and Processing of Numbers" ” has been said before, so I won’t go into details here.
Considering the load of the browser, here, I only allow the odd number entered by the user to be 189. You can also increase it a bit. 189 feels okay in my computer, so I set this number. It has no special meaning. It was entered randomly and was not intentionally tested.
Enter a line of 189, and IE has already popped up a prompt to "abort the script", but there is no problem if you don't abort!
The running results are as follows:
If you are writing a C language program or other programs, this value can definitely be set larger!
2. Basic Idea
Once you understand this, it is very easy to write.
First of all, we only need to output spaces on the left side of *. There is no need to output spaces on the right side. After typing *, just change the line directly.
Split into two Parts, one part is when in/2 in the lower half.
The reason why it is divided like this is because the * sign in the output of these two parts It is different from the number of spaces output.
After that, it was a common problem in junior high school to find the rules. Anyway, I found the above rules, and there was no problem when programming and running. Other math emperors found more awesome expressions, and I was defeated.
3. Production process
The code is very simple, it is the expression of conditional structure and loop structure. It goes without saying that the above ideas are understood. .
Some people may find it strange here, why should I first judge n++ and then n%2!=0, that is, judge whether n+1 is an even number to judge whether n is an odd number,
The main thing here is It is to cater to the following for loop structure...
<html> <head> <meta http-equiv="content-type" content="text/html;charset=gb2312"/> </head> <body> </body> </html> <script> var i,j,k,n; n=window.prompt("请输入要输出的行数n,为了形成轴对称,所以你输出的必须是奇数!"); if(isNaN(n)||!n) alert("你输入的不是数!"); else{ n++; if(n%2!=0) alert("你输入的不是奇数!"); else if(n>190){ alert("不要这么大嘛!臣妾做不到啊!"); } else{ for(i=1;i<n;i++){ if(i<=n/2){ for(k=n/2-i;k>0;k--) document.write("&nbsp;"); for(j=0;j<2*i-1;j++) document.write("*"); } else{ for(k=i-n/2;k>0;k--) document.write("&nbsp;"); for(j=0;j<2*(n-i)-1;j++) document.write("*"); } document.write("<br>"); } } } </script>
It will make me cry if I say too much, I will experience it myself...
The above is the detailed content of Implementation example of JavaScript printing star pyramid function. For more information, please follow other related articles on the PHP Chinese website!

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

How to send task notifications in Quartz In advance When using the Quartz timer to schedule a task, the execution time of the task is set by the cron expression. Now...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment