JavaScript tutorial 73 – Print pyramid patterns of numbers and stars




JavaScript tutorial 73 – Print pyramid patterns of numbers and stars

Print pyramid patterns of numbers and stars
Displaying patterns using javascript nested for loop:

var rows=5;

for(var i=1;i<=rows;i++)
{
for(var j=1;j<=i;j++)
{
document.write(” * “);
}
document.write(“<br/>”);
}
Output:
*
* *
* * *
* * * *
* * * * *

Displaying pyramid using for loop:

var rows = 5;

for(var i=1; i<=rows; i++)
{
for(var k=1; k<=( rows-i ); k++)
{
document.write(” “);
}
for(var j=1; j<=i; j++)
{
document.write(“* “);
}
document.write(“<br/>”);
}
Output:
*
* *
* * *
* * * *
* * * * *

=========================================

Follow the link for next video:
JavaScript tutorial 74 – function declaration | functions in javascript

Follow the link for previous video:
JavaScript tutorial 72 – Print patterns of numbers and stars

====== JavaScript Questions & Answers ========

=========================================

Original source


31 responses to “JavaScript tutorial 73 – Print pyramid patterns of numbers and stars”

  1. Thank you so much for this long but really clear explanation I never understood how nested for loops worked but since you went over every single bit of code I now understand it completly. Thank you and god bless you sir.

  2. <html>

    <body>
    <h2>JavaScript Loops</h2>
    <script>
    var rows = 5;
    var cols = 5;

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

    for (var k = 1; k<=(rows-i); k++){
    document.write("&nbsp;");
    }

    for (var j = 1; j <=i; j++) {
    document.write("*");
    }

    document.write("<br>");

    }
    </script>
    </body>
    </html>
    <!– // OUT PUT
    JavaScript Loops
    *
    **
    *
    **
    // –>

  3. Hey here's mine and there is no pyramid`

    var rows = 5
    // var cols = 5
    for (var i = 1; i <= rows; i++) {
    for (var k = 1; i <= ( rows-i ); k++){
    document.write("&nbsp;")
    }
    for (var j = 1; j <= i; j++) {
    document.write("* ");
    }
    document.write("<br>");
    }

  4. incorrect code posted in description. this is the pyramid one:
    <script type= "text/javascript">
    var rows = 5;

    for(var i=1; i<=rows; i++)
    {
    for(var k=1; k<=( rows-i ); k++)
    {
    document.write("&nbsp;");
    }
    for(var j=1; j<=i; j++)
    {
    document.write("* ");
    }
    document.write("<br/>");
    }

Leave a Reply