• We just launched and are currently in beta. Join us as we build and grow the community.

How to Generate Pyramid Shape Object in JavaScript

loremipsum

Threat Hunter
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
184
Likes
169
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Generate Pyramid Shape Object in JavaScript

Introduction

In this tutorial we will create a How to Generate Pyramid Shape Object in JavaScript. This tutorial purpose is to teach you on how to create a pyramid shape object. This will cover all the important functionality that will generate a pyramid. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application to showcase some object. I will give my best to provide you the easiest way of creating this program Generate Pyramid Object. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only html form and the pyramid object. To create this simply copy and write it into your text editor, then save it 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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Generate Pyramid Shape Object</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <form

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <label

    >

    Enter the pyramid size</

    label

    >
  21. <input

    type

    =

    "number"

    id

    =

    "row"

    class

    =

    "form-control"

    /

    >
  22. </

    div

    >
  23. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary btn-block"

    onclick

    =

    "generatePyramid();"

    >

    Generate</

    button

    >
  24. </

    form

    >
  25. </

    div

    >
  26. <div

    class

    =

    "col-md-2"

    ><h2

    >

    RESULT</

    h2

    ></

    div

    >
  27. <div

    class

    =

    "col-md-6"

    >
  28. <div

    id

    =

    "result"

    ></

    div

    >
  29. </

    div

    >
  30. </

    div

    >
  31. <script

    src

    =

    "script.js"

    ></

    script

    >
  32. </

    body

    >
  33. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will generate a pyramid shape object after submitting the form. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    generatePyramid(

    )

    {
  2. var

    row=

    document.getElementById

    (

    'row'

    )

    .value

    ;

  3. if

    (

    row ==

    ""

    )

    {
  4. alert(

    "Please enter something first!"

    )

    ;
  5. }

    else

    {
  6. var

    html=

    ""

    ;
  7. for

    (

    var

    i=

    1

    ;

    i<=

    row;

    i++

    )

    {
  8. for

    (

    var

    k=

    1

    ;

    k<=

    (

    row-

    i)

    ;

    k++

    )

    {

  9. html+=

    "&nbsp;"

    ;
  10. }

  11. for

    (

    var

    j=

    1

    ;

    j<=

    i;

    j++

    )

    {
  12. html+=

    "| "

    ;
  13. }

  14. html+=

    "<br />"

    ;
  15. }

  16. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    html;
  17. }
  18. }

This code above only create one method called generatePyramid(), this function will generate a pyramid shape object after you submit the size number. This method is looping to loop through the element and positioning in a pyramid pattern

Output:

How%20to%20Generate%20Pyramid%20Shape%20Object%20in%20JavaScript%201_0.png


The How to Generate Pyramid Shape Object in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Generate Pyramid Shape Object in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,348

323,357

Top