How To Create Left And Right Triangle Pattern

In this topic,

We will see how to create “Left” & “Right” patterns.

Step-1: include the jQuery link in the <head>..</head>tag on the index.html file.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Step-2: Create Html Code in the index.html file.

<body>
  <p id="left_pattern_output"></p>
  
  <p id="right_pattern_output"></p>
</body>

Step-3: Add jQuery code in <script>…</script> tag on index.html.

For Left Triangle:

$(document).ready(function() {
    var i;
    var j ,text="";
    for(i = 0; i < 5; i++) { 
      for(j = 0; j <=i; j++) {
      text+="*";
      }
      text +="<br>";
    }    
    $("#left_pattern_output").html(text);
});

Output Left Triangle Pattern:

Right Left Triangle:

$(document).ready(function() {
    var i;
    var j ,text="";
    for(i = 0; i < 5; i++) { 
      for(j = 5; j >=0; j--) {
        if(i>=j){
          text+= "*";
        }
        else{
          text += " &nbsp;";
        }
      }
      text +="<br>";
    }  
    $("#demo").html(text);
});

Output Left Triangle Pattern:

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories