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

asp*_*pGk -2 javascript for-loop

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

for(i=5;i>=1;i--) {
    for(j=i;j>=1;j--)
        console.log("%d",j);
        console.log("\n");
}
Run Code Online (Sandbox Code Playgroud)

小智 15

  for(var i=1; i<=4; i++){
       console.log("* ".repeat(i));
    }
Run Code Online (Sandbox Code Playgroud)

/*
Output is: 
"*"
"* *"
"* * *"
"* * * *"
*/
Run Code Online (Sandbox Code Playgroud)


小智 9

for (var line = "#"; line.length < 8; line += "#")
console.log(line);
Run Code Online (Sandbox Code Playgroud)


小智 7

  
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Nve*_*yan 5

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

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


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

            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)
Run Code Online (Sandbox Code Playgroud)


Roh*_*wad 5

这是我只使用一个 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--;
}
Run Code Online (Sandbox Code Playgroud)

输出:

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


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

---------------------------*/
Run Code Online (Sandbox Code Playgroud)