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

How to Create Multiplication Table in JavaScript

clamarsa

System Deception Specialist
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
34
Likes
198
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Introduction

In this tutorial we will create a How to Create Multiplication Table in JavaScript. This tutorial purpose is to give a simple tool that can generate a multiplication table. This will tackle the multiplying of integers. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is very easy to understand just follow the instruction I provided and you will do it without a problem. This program can be use when you want to learn how to multiply numbers. I will try my best to give you the easiest way of creating this program Multiplication Table. So let's do the coding.

Before we get started:

This 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 display an html form that we will be needed to submit. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    >
  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

    href

    =

    "https://sourcecodester.com"

    class

    =

    "navbar-brand"

    >

    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 Create Multiplication Table</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted;"

    /

    >
  17. <div

    class

    =

    "col-md-3"

    >
  18. <div

    class

    =

    "form-group"

    >
  19. <label

    >

    Enter a number</

    label

    >
  20. <input

    type

    =

    "number"

    class

    =

    "form-control"

    id

    =

    "number"

    /

    >
  21. <br

    /

    >
  22. <button

    onclick

    =

    "displayTable()"

    class

    =

    "btn btn-primary brn-block"

    >

    Create Table</

    button

    >
  23. </

    div

    >
  24. </

    div

    >
  25. <div

    class

    =

    "col-md-9 table-responsive"

    >
  26. <table

    class

    =

    "table table-bordered"

    >
  27. <thead

    class

    =

    "alert-info"

    >
  28. <tr

    >
  29. <th

    colspan

    =

    "10"

    ><center>

    Multiplication Table</

    center></

    th

    >
  30. </

    tr

    >
  31. </

    thead

    >
  32. <tbody

    id

    =

    "data"

    style

    =

    "background-color:#fff;"

    ></

    tbody

    >
  33. </

    table

    >
  34. </

    div

    >
  35. </

    div

    >
  36. </

    body

    >
  37. </

    html

    >

  38. <script

    src

    =

    "script.js"

    ></

    script

    >

Creating JavaScript Function

This is where the main function of the application is. This code will generate a multiplication table when an event is trigger. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. document.getElementById

    (

    "number"

    )

    .onkeyup

    =

    function

    (

    )

    {
  2. var

    input=

    parseInt(

    this

    .value

    )

    ;

  3. if

    (

    input >

    20

    )

    {
  4. this

    .value

    =

    20

    ;
  5. }
  6. if

    (

    input <

    0

    )

    {
  7. this

    .value

    =

    1

    ;
  8. }

  9. }


  10. function

    displayTable(

    )

    {
  11. var

    number =

    document.getElementById

    (

    'number'

    )

    .value

    ;


  12. if

    (

    number ==

    ""

    )

    {
  13. alert(

    "Enter some number first"

    )

    ;
  14. }

    else

    {
  15. var

    digit =

    0

    ;
  16. if

    (

    number <=

    0

    )

    {
  17. digit =

    1

    ;
  18. document.getElementById

    (

    'number'

    )

    .value

    =

    1

    ;
  19. }

    else

    if

    (

    number >

    20

    )

    {
  20. digit =

    20

    ;
  21. document.getElementById

    (

    'number'

    )

    .value

    =

    20

    ;
  22. }

    else

    {
  23. digit =

    number;
  24. }



  25. var

    data =

    ""

    ;
  26. for

    (

    var

    i =

    1

    ;

    i <=

    digit;

    i++

    )

    {
  27. data +=

    "<tr>"

    ;
  28. for

    (

    var

    j =

    1

    ;

    j <=

    10

    ;

    j++

    )

    {

  29. data +=

    "<td>"

    +

    i*

    j+

    "</td>"

    ;
  30. }
  31. data +=

    "</tr>"

    ;
  32. }

  33. document.getElementById

    (

    'data'

    )

    .innerHTML

    =

    data;
  34. }
  35. }

In this code we first restrict the maximum value that can be submitted in the textbox by 20. We then bind the textbox value into a variable and set its name into number.

To generate the multiplication table, we increment the submitted value using nested loop with a maximum length of 10 in order to get the multiplied numbers and display it into a html content.

Output:

How%20to%20Create%20Multiplication%20Table%20in%20JavaScript%201.png


The How to Create Multiplication Table 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 Create Multiplication Table 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 the hidden content.
 

452,292

324,736

324,744

Top