search
HomeWeb Front-endJS TutorialJavaScript print star pyramid function
JavaScript print star pyramid functionJan 30, 2018 pm 01:19 PM
javascriptjspyramid

This article mainly introduces JavaScript to realize the function of printing star pyramid. It 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 who need it can refer to it. I hope it can help. Everyone.

It is exactly the same for you to write in other languages.

I guess everyone has seen this question when they were learning C language...

That is, print the following The ghost thing:

When I saw the loop structure, I felt very bored, so I disdained this question and didn’t think about it carefully, 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 utf-8 encoding is not used here is because utf-8 is suitable for nbsp's processing and * font will cause layout confusion, that is, the standard Song font is not used, making 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 sake of 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 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. 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 do I need to judge n++ first and then n%2!=0, that is, judging 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>

Related recommendations:

JavaScript method of printing debugging information (Jquery)

Javascript script to print part of the content of the web page_javascript skills

JavaScript print iframe content sample code_javascript skills

The above is the detailed content of JavaScript print star pyramid function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.