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

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

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

This question is related to javascript for-loop

The answer is


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

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


Try this. Maybe it will work for you:

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

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


_x000D_
_x000D_
for (var i = 7; i >= 1; i--) {_x000D_
  var str = "";_x000D_
  for (var j = i; j <= 7; j++) {_x000D_
  str += "*";_x000D_
     }_x000D_
 console.log(str);_x000D_
}_x000D_
// This is example_x000D_
_x000D_
// You can do this with any string and without using the function. 
_x000D_
_x000D_
_x000D_


<html>
<head>
<script>
    //Declare Variable
    var i, j;
    //outer loop
    for(i = 0; i <= 25; i++){
    //inner loop
    for(j = 0; j <= i; j++){
    document.write("*");
    }
    document.write('<br>');
    }
</script>
</head>
<body>
</body>
</html>

<!-- end snippet -->

the log will output to a new line every time it is called, in chrome if it's the same it will just keep a count (not sure about other browsers). You want to collect the number of stars per line then output that after the inner loop has run

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


Try this one for diamond pattern in javascript

<head>
 <style>
    p{text-align:center;margin-left:20px;}
 </style>
</head>

<body>
   <h1>JavaScript patterns</h1>
   <p id="demo"></p>

<script>
  var x=function(n){
    document.write("<center>");

  var c="";
    for(var i=0; i<n; i++){
      c=c+"#";
      document.write(c);
      document.write("<br>");  
   }

    for(var k=n;k>0;k--){
      for(var j=0; j<(k-1); j++){
       document.write("#");
      } 
    document.write("<br>");
   }   
  }
     document.getElementById("demo").innerHTML = x(10);

</script>


function pyramid(n) {

    for(i=1 ;i<=n;i++) {

        let str = ' '.repeat(n-i);
        let str2 = '*'.repeat(i*2-1);
    console.log(str + str2 + str);
    }
}

pyramid(5)

This below code worked for me

for(i= 0; i< n; i++){

   col = i;
   for(j = 0; j< n- col;j++){
       process.stdout.write('');
   }
   for (j = 1;j< col+2;j ++){
       process.stdout.write('#');
   }
   process.stdout.write('\n');
}

_x000D_
_x000D_
            /** --------------_x000D_
_x000D_
                    *_x000D_
                   **_x000D_
                  ***_x000D_
                 ****_x000D_
                *****_x000D_
               ******_x000D_
              *******_x000D_
             ********_x000D_
            *********_x000D_
_x000D_
_x000D_
            ----------------*/_x000D_
_x000D_
            let y = 10;_x000D_
            let x = 10;_x000D_
_x000D_
            let str = "";_x000D_
_x000D_
            for(let i = 1; i < y; i++ ){_x000D_
                for(let j = 1; j < x; j++){_x000D_
                    if(i + j >= y){_x000D_
                        str = str.concat("*");_x000D_
                    }else{_x000D_
                        str = str.concat(" ")_x000D_
                    }_x000D_
                }_x000D_
                str = str.concat("\n")_x000D_
            }_x000D_
_x000D_
            console.log(str)_x000D_
_x000D_
_x000D_
            /**________________________x000D_
_x000D_
_x000D_
_x000D_
            *********_x000D_
             ********_x000D_
              *******_x000D_
               ******_x000D_
                *****_x000D_
                 ****_x000D_
                  ***_x000D_
                   **_x000D_
                    *_x000D_
_x000D_
_x000D_
             _______________________*/_x000D_
_x000D_
            let str2 = "";_x000D_
_x000D_
            for(let i = 1; i < y; i++ ){_x000D_
                for(let j = 1; j < x; j++){_x000D_
                    if(i <= j ){_x000D_
                        str2 = str2.concat("*");_x000D_
                    }else{_x000D_
                        str2 = str2.concat(" ")_x000D_
                    }_x000D_
                }_x000D_
                str2 = str2.concat("\n")_x000D_
            }_x000D_
_x000D_
            console.log(str2)_x000D_
_x000D_
_x000D_
            /**----------------------_x000D_
_x000D_
_x000D_
            *_x000D_
            **_x000D_
            ***_x000D_
            ****_x000D_
            *****_x000D_
            ******_x000D_
            *******_x000D_
            ********_x000D_
_x000D_
_x000D_
             -------------------------*/_x000D_
_x000D_
_x000D_
            let str3 = "";_x000D_
_x000D_
            for(let i = 1; i < y; i++ ){_x000D_
                for(let j = 1; j < x; j++){_x000D_
                    if(i >= j ){_x000D_
                        str3 = str3.concat("*");_x000D_
                    }_x000D_
                }_x000D_
                str3 = str3.concat("\n")_x000D_
            }_x000D_
_x000D_
            console.log(str3)_x000D_
_x000D_
            /**-------------------------_x000D_
_x000D_
_x000D_
             *********_x000D_
             ********_x000D_
             *******_x000D_
             ******_x000D_
             *****_x000D_
             ****_x000D_
             ***_x000D_
             **_x000D_
             *_x000D_
_x000D_
             ---------------------------*/_x000D_
            let str4 = "";_x000D_
_x000D_
            for(let i = 1; i < y; i++ ){_x000D_
                for(let j = 1; j < x; j++){_x000D_
                    if( j >= i ){_x000D_
                        str4 = str4.concat("*");_x000D_
                    }_x000D_
                }_x000D_
                str4 = str4.concat("\n")_x000D_
            }_x000D_
_x000D_
            console.log(str4)_x000D_
_x000D_
            /**--------------------_x000D_
             Diamond of Asterisks_x000D_
_x000D_
                 *_x000D_
                ***_x000D_
               *****_x000D_
              *******_x000D_
             *********_x000D_
              *******_x000D_
               *****_x000D_
                ***_x000D_
                 *_x000D_
_x000D_
_x000D_
             ---------------------*/_x000D_
_x000D_
            let str5 = "";_x000D_
_x000D_
            for(let i = 1; i < y; i++ ){_x000D_
                for(let j = 1; j < x; j++){_x000D_
                    if(i <= y / 2 && j >= (y / 2) - (i - 1) && j <= (y / 2) + (i - 1) ){_x000D_
                        str5 = str5.concat("*");_x000D_
                    }else if(i >= y / 2_x000D_
                      && j > ((y / 2) -  i) * (-1)_x000D_
                      && j < (y - ((y / 2) -  i) * (-1))){_x000D_
                        str5 = str5.concat("*");_x000D_
                    }_x000D_
                    else {_x000D_
                        str5 = str5.concat(" ");_x000D_
                    }_x000D_
                }_x000D_
                str5 = str5.concat("\n");_x000D_
            }_x000D_
_x000D_
            console.log(str5)
_x000D_
_x000D_
_x000D_


Here is the solution in javascript while loop:

> var i = 0, out = '';
>  while( i <= 4)
>     {
>       out = out + '* ';
>       document.write('<br> '+ out);
>       i++;
>     } 
>  
> document.write('<br>');

_x000D_
_x000D_
for(i=0;i<=5;i++)_x000D_
{_x000D_
    for(j=0;j<=i;j++)_x000D_
    {_x000D_
        document.write('*');_x000D_
    }_x000D_
    document.write('<br>')_x000D_
}
_x000D_
_x000D_
_x000D_


You can try this

 var x, y, space = "",
    star = "",
    n = 4,
    m = n - 1;
for (x = 1; x <= n; x++) {
    for (y = m; y >= 1; y--) {
        space = space + (" ");
    }
    m--;
    for (let k = 1; k <= x * 2 - 1; k++) {
        star = star + "*"
    }
    console.log(space + star)
    space = '';
    star = "";
}

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/>");
}

for(var a=1;a<8;a++){   
    var o="";  
    for(var b=1;b<=a;b++){  
        o +="#";    
    }   
    debug(o);   
}

Try above code.

Output:

--> #
--> ##
--> ###
--> ####
--> #####
--> ######

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);

<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>
        <p id="test"></p>
    </body>
    <script>
        //Declare Variable
        var i;
        for(i = 0; i <= 5; i++){
            document.write('*'.repeat(i).concat("<br>"))
        }
    </script>
</html>

<!DOCTYPE html>
<html>
    <head>
    <script>
        //Declare Variable
        var i,j;

        //First Way
        for(i = 5; i >= 0; i--){
            for(j = 0; j <= i; j++){
                document.write('*');
            }
            document.write('<br>');
        }
        //Second Way
        for(i = 5; i >= 0; i--){
            document.write('*'.repeat(i).concat('<br>'))
        }
    </script>
    </head>
    <body>
    </body>
</html>

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

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

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

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

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/


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

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:

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


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

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