javascript-tutorialsmade

JavaScript to print Diamond pattern!

Hello People, here is another interesting script for you!. Usually these kind of programs you could see in C language only, but in our blog I’m writing these pattern program in PHP, JavaScript, Java, Python etc.,

Today’s less is about how to print Diamond pattern in JavaScript!

See the demo here:

demo

See the script here:

Output of the above script will be this:

diamond-pattern-php

Enjoy the day!

Related Posts

5 thoughts on “JavaScript to print Diamond pattern!

  1. *
    **
    ***
    ****
    *****
    Can you make this pattern in a single loop only? This question is asked in my interview.

  2. I used the Absolute function to make it shorter 🙂
    //make sure the width is an ODD number to get perfect diamond
    var stars=””;
    var width = 11;
    var num = (width+1)/2;
    for (let i = num-1; i >-num; i–) {
    for (let j = num-Math.abs(i); j < num; j++) {
    stars+=' '
    }
    for (let j = 0; j < 2*(num-Math.abs(i))-1; j++) { //2*num-(2*Math.abs(i) +1) simplified to 2*(num-Math.abs(i))-1
    stars+="*";
    }
    stars+="\n";
    }
    console.log(stars);

  3. document.write(“”)
    for(i=1; i <= 7; i++) {
    for(j=1; j<=i; j++) {

    document.write("*");
    }
    document.write("”);
    }
    for(i=1; i =i; j–) {
    if (j==7)
    {
    continue
    }
    document.write(“*”)
    }
    document.write(“”)
    }
    document.write(“”)

Leave a Reply