"JavaScript" Print star pyramid __java

Source: Internet
Author: User

It's exactly the same thing to make you write in other languages,

This problem is estimated that everyone in the learning of C language when they have seen ...

Which is to print the following ghost things:


When the time to see the structure of the cycle is very boring, rather disdain this problem, did not think carefully,

Because if you can write it in JavaScript, it's not even JavaScript, it's just an HTML:

The reason why the UTF-8 encoding is not used is because Utf-8 's handling of nbsp and the font of * will cause typographical disorder, which is not a standard italicized word, resulting in the final result cannot be viewed.

Anyway, you have a question how many lines, I can copy and paste the above content can be completely realized, however, today's programming to a certain amount, a bit of large data concepts, and see this problem, think carefully about it, if the subject asked to output 200,000 lines such as the middle Axis symmetry star pyramid, I TMD oh.

So we have to figure out how this is done, although in the actual programming will not appear such programming, but, it is said that some boring interview or will be out of this problem, at least, later asked you C language, if you output 200,000 lines such as the middle Axis symmetry star pyramid, you still want to, If you do something bad for the first time.


I. Basic objectives

First pop-up an input box, let the user input an odd number, after all, axisymmetric need an odd number,

Then for the robustness of the program, you must judge the input of the user, if the input is not odd, pop-up prompts, no longer execute the following program, how JavaScript judge an odd number, I have in the "JavaScript" logarithm of the judge and logarithmic processing (click to open the link) said , no more repeat.

Take into account the load of the browser, here, the user input the odd number, I only allow to go to 189, you can increase a little bit, 189 because in my computer inside feel can also, so only to set this count, no special significance, random input, not deliberately tested.

Enter a 189 line, IE has popped the "abort script" prompt, but not stop is completely no problem.

The results of the operation are as follows:


If you are writing C language programs or other programs, this value can definitely be a bit larger.


Ii. Basic Ideas

This is very good to understand and write.


First of all, we just want to output space on the left side of the *, there is no need to output space on the right, after the loss of the * will be directly changed line

It is divided into two parts, one part is the I<=N/2 of the upper part, and the other part is the I>N/2 of the lower part.

The reason for this is that these two parts of the output of the * number and the number of spaces are different from the output.

After that is the junior high school will find the law problem, anyway, I found as the law, programming run no problem, other math Emperor Find more cow B expression, younger brother to defeat.


third, the production process

The code is very simple, is the conditional structure and the expression of the circular structure, the above thought understand, needless to say.

There may be people wondering why I have to n++ and then n%2!=0 to judge that N+1 is even to determine if n is odd,

This is mainly to cater to the following for loop structure ...

 

Say more is also tears, own experience ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.