JavaScript - Display Pyramid Star

In this tutorial we will create a Display Pyramid Star using JavaScript. This code will display a pyramid of star when user submit the total row of the pyramid. This code will use onclick() to call a specific function that can display a pyramid made of star using for() loop by nesting the value of the main loop. You can apply this script to your code, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user. .

Getting Started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Display Pyramid Star</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-4">
  17.                         <form>
  18.                                 <div class="form-group">
  19.                                         <label>Enter a row</label>
  20.                                         <input type="number" id="row" class="form-control" />
  21.                                 </div>
  22.                                 <center><button type="button" class="btn btn-primary" onclick="displayPyramid();">Display</button></center>
  23.                         </form>
  24.                 </div>
  25.                 <div class="col-md-2"></div>
  26.                 <div class="col-md-6">
  27.                         <div id="result"></div>
  28.                 </div>
  29.         </div>
  30. <script src="js/script.js"></script>   
  31. </body>
  32. </html>

Creating the Script

This code contains the script of the application. This code will display pyramid of star when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function displayPyramid(){
  2.         var row=document.getElementById('row').value;
  3.        
  4.         if(row ==""){
  5.                 alert("Please enter something first!");
  6.         }else{
  7.                 var html="";
  8.                 for(var i=1; i<=row; i++){
  9.                         for(var k=1; k<=(row-i); k++){
  10.                        
  11.                                 html+="&nbsp;";
  12.                         }
  13.                        
  14.                         for(var j=1; j<=i; j++){
  15.                                 html+="* ";
  16.                         }
  17.                        
  18.                         html+="<br />";
  19.                 }
  20.                
  21.                 document.getElementById('result').innerHTML=html;
  22.         }
  23. }
There you have it we successfully created a Display Pyramid Star using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment