JavaScript - Simple Multiplication Table

In this tutorial we will create a Simple Multiplication Table using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

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.         <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">Javascript - Simple Multiplication Table</h3>
  14.                 <hr style="border-top:1px dotted;"/>
  15.                 <div class="form-inline">
  16.                         <label>Enter a number</label>
  17.                         <input type="number" class="form-control" id="number"/>
  18.                         <button onclick="displayTable()" class="btn btn-primary">Create Table</button>
  19.                 </div>
  20.                 <br />
  21.                 <table class="table table-bordered">
  22.                         <thead class="alert-info">
  23.                                 <tr>
  24.                                         <th colspan="10"><center>Multiplication Table</center></th>
  25.                                 </tr>
  26.                         </thead>
  27.                         <tbody id="data" style="background-color:#fff;"></tbody>
  28.                 </table>
  29.         </div>
  30. </body>
  31. </html>
  32.  
  33. <script src="js/script.js"></script>

Creating the Script

This code contains the script of the application. This code will generate a multiplication when user enter some inputs. 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 folder.
  1. document.getElementById("number").onkeyup=function(){
  2.         var input=parseInt(this.value);
  3.        
  4.         if(input > 10){
  5.                 this.value = 10;
  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 > 10){
  26.                         digit = 10;
  27.                         document.getElementById('number').value = 10;
  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. }
There you have it we successfully created a Simple Multiplication Table 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