如何以非常简单的方式在 JavaScript 中打印星形图案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28365737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:41:04  来源:igfitidea点击:

How to print star pattern in JavaScript in a very simple manner?

javascriptfor-loop

提问by aspGk

I have tried the code below but it's output is not proper!

我已经尝试了下面的代码,但它的输出不正确!

for(i=5;i>=1;i--) {
    for(j=i;j>=1;j--){
        console.log(j);
    }
        console.log("\n");
}

回答by user9522108

  
<html>

<head>
<script type="text/javascript">
 var i,j;
 for(i=1; i <= 5; i++)
 {
  for(j=1; j<=i; j++)
 {
   
   document.write('*');
  }
   document.write('<br />');
  }
    
</script>
</head>
<body>
</body>
</html>

回答by RK_oo7

for(var i=1; i<=4; i++){
   console.log("*".repeat(i));
}

/*
Output is: 
"*"
"**"
"***"
"****"
*/

回答by Nver Abgaryan

            /** --------------

                    *
                   **
                  ***
                 ****
                *****
               ******
              *******
             ********
            *********


            ----------------*/

            let y = 10;
            let x = 10;

            let str = "";

            for(let i = 1; i < y; i++ ){
                for(let j = 1; j < x; j++){
                    if(i + j >= y){
                        str = str.concat("*");
                    }else{
                        str = str.concat(" ")
                    }
                }
                str = str.concat("\n")
            }

            console.log(str)


            /**_______________________



            *********
             ********
              *******
               ******
                *****
                 ****
                  ***
                   **
                    *


             _______________________*/

            let str2 = "";

            for(let i = 1; i < y; i++ ){
                for(let j = 1; j < x; j++){
                    if(i <= j ){
                        str2 = str2.concat("*");
                    }else{
                        str2 = str2.concat(" ")
                    }
                }
                str2 = str2.concat("\n")
            }

            console.log(str2)


            /**----------------------


            *
            **
            ***
            ****
            *****
            ******
            *******
            ********


             -------------------------*/


            let str3 = "";

            for(let i = 1; i < y; i++ ){
                for(let j = 1; j < x; j++){
                    if(i >= j ){
                        str3 = str3.concat("*");
                    }
                }
                str3 = str3.concat("\n")
            }

            console.log(str3)

            /**-------------------------


             *********
             ********
             *******
             ******
             *****
             ****
             ***
             **
             *

             ---------------------------*/
            let str4 = "";

            for(let i = 1; i < y; i++ ){
                for(let j = 1; j < x; j++){
                    if( j >= i ){
                        str4 = str4.concat("*");
                    }
                }
                str4 = str4.concat("\n")
            }

            console.log(str4)

            /**--------------------
             Diamond of Asterisks

                 *
                ***
               *****
              *******
             *********
              *******
               *****
                ***
                 *


             ---------------------*/

            let str5 = "";

            for(let i = 1; i < y; i++ ){
                for(let j = 1; j < x; j++){
                    if(i <= y / 2 && j >= (y / 2) - (i - 1) && j <= (y / 2) + (i - 1) ){
                        str5 = str5.concat("*");
                    }else if(i >= y / 2
                      && j > ((y / 2) -  i) * (-1)
                      && j < (y - ((y / 2) -  i) * (-1))){
                        str5 = str5.concat("*");
                    }
                    else {
                        str5 = str5.concat(" ");
                    }
                }
                str5 = str5.concat("\n");
            }

            console.log(str5)

回答by Rahul Rai

It's very simple, Try this code as below:

很简单,试试下面的代码:

for(var i = 1; i <= 5; i++) {

      for(var j = 1; j<= i; j++) {

        document.write("*");  

      }

      document.write("<br/>");
}

回答by Mwongera808

for (var line = "#"; line.length < 8; line += "#")
console.log(line);

回答by Rohit Gaikwad

This is the simplest solution which I came across using only one for loop.

这是我只使用一个 for 循环遇到的最简单的解决方案。

var a = '';
var n = 5;
var m = (n-1); 
for(i=1; i <= n; i++)
{
    a = a.trim();
    a = ' '.repeat(m) + a + (i > 1 ? ' ' : '') + '*';
    console.log(a);
    m--;
}

Output:

输出:

/**------------------------


    *
   * *
  * * *
 * * * *
* * * * *

---------------------------*/

回答by Rohit Gaikwad

for (var i = 7; i >= 1; i--) {
  var str = "";
  for (var j = i; j <= 7; j++) {
  str += "*";
     }
 console.log(str);
}
// This is example

// You can do this with any string and without using the function. 

回答by Amanverma Lucky

for (let i = 1; i <= 5; i++) {
    for (let j = 1; j <= i; j++) {
        document.write('*');
    }
    document.write('<br />');
}

回答by Arul

Just try it out

试试看

**Your Pyramid will be downwards like: **

**你的金字塔会向下像:**

4 3 2 1
 3 2 1
  2 1
   1

function stars(n){
    var str = '';
    for(var i=n; i>=1; i--){
        for(var k=n; k>=i; k--){
            str += "\t";
        }
        for(var j=i; j>=1; j--){
            str += j+"\t\t";
        }
        console.log(str);
        str = "";
    }
}
stars(3);

Your Pyramid will be upwards like :

你的金字塔会像这样向上:

  *
 * *
* * *

function stars(n){
    var str = '';
    for(var i=1; i<=n; i++){
        for(var k=1; k<=n-i; k++){
            str += "\t";
        }
        for(var j=1; j<=i; j++){
            str += "*\t\t";
        }
        console.log(str);
        str = "";
    }
}
stars(3);

回答by zavg

As I understand from your code, you are actually trying to print stairpattern rather than star.

正如我从您的代码中了解到的,您实际上是在尝试打印楼梯图案而不是star

Your main error consists in that console.logfunction prints every time on the next line.

您的主要错误在于该console.log函数每次都在下一行打印。

for (var i = 5; i >= 1; i--) {
    var str = "";
    for (var j = i; j >= 1; j--) str += j;
    console.log(str);
}

JSFiddle for you: http://jsfiddle.net/99wL8cbt/2/

您的 JSFiddle:http: //jsfiddle.net/99wL8cbt/2/