How to print a triangle formed of '#' using JavaScript?

JavascriptWeb DevelopmentFront End Technology

To print a triangle formed of “#”, you need to use a nested “for loop”.

Example

Try the following code to print a triangle −

Live Demo

<html>
   <head>
      <body>
         <p>Printing a triangle</p>
         <script>
            var i, j;
            for(i=1; i < 5; i++) {
               //inner loop
              for(j=1; j<=i; j++) {
                 document.write('#');
            }
            document.write('<br/>');
         }
      </script>
   </body>
</html> 

Output

Printing a triangle
#
##
###
####
raja
Published on 03-Feb-2018 10:45:34
Advertisements