How to Create Multiplication Table in JavaScript

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. <head>
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">How to Create Multiplication Table</h3>
  14. <hr style="border-top:1px dotted;"/>
  15. <div class="col-md-3">
  16. <div class="form-group">
  17. <label>Enter a number</label>
  18. <input type="number" class="form-control" id="number"/>
  19. <br />
  20. <button onclick="displayTable()" class="btn btn-primary brn-block">Create Table</button>
  21. </div>
  22. </div>
  23. <div class="col-md-9 table-responsive">
  24. <table class="table table-bordered">
  25. <thead class="alert-info">
  26. <tr>
  27. <th colspan="10"><center>Multiplication Table</center></th>
  28. </tr>
  29. </thead>
  30. <tbody id="data" style="background-color:#fff;"></tbody>
  31. </table>
  32. </div>
  33. </div>
  34. </body>
  35. </html>
  36.  
  37. <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.  
  4. if(input > 20){
  5. this.value = 20;
  6. }
  7. if(input < 0){
  8. this.value = 1;
  9. }
  10.  
  11. }
  12.  
  13.  
  14. function displayTable(){
  15. var number = document.getElementById('number').value;
  16.  
  17.  
  18. if(number == ""){
  19. alert("Enter some number first");
  20. }else{
  21. var digit = 0;
  22. if(number <= 0){
  23. digit = 1;
  24. document.getElementById('number').value = 1;
  25. }else if(number > 20){
  26. digit = 20;
  27. document.getElementById('number').value = 20;
  28. }else{
  29. digit = number;
  30. }
  31.  
  32.  
  33.  
  34. var data = "";
  35. for(var i = 1; i <= digit; i++) {
  36. data += "<tr>";
  37. for(var j = 1; j <= 10; j++){
  38.  
  39. data += "<td>"+i*j+"</td>";
  40. }
  41. data += "</tr>";
  42. }
  43.  
  44. document.getElementById('data').innerHTML = data;
  45. }
  46. }

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:

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

Add new comment