Generate Multiplication Table Using JavaScript

In this tutorial we will create a Generate Multiplication Table using JavaScript. This code will dynamically display a multiplied numbers in a table when user click the generate button. The code use onclick() function to call a specific function that dynamically display a table of multiplication using a nested for() loops in order to loop through number by multiplying as a counting numbers. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

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 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">Generate Multiplication Table Using JavaScript Source Code</h3>
  14. <hr style="border-top:1px dotted;"/>
  15. <div class="col-md-4">
  16. <div class="form-group">
  17. <label>Enter a number</label>
  18. <input type="number" class="form-control" id="number"/>
  19. </div>
  20. <center><button onclick="displayTable()" class="btn btn-primary">Generate</button></center>
  21. </div>
  22. <br />
  23. <div class="col-md-8">
  24. <table class="table table-bordered">
  25. <tbody id="data" style="background-color:#fff;"></tbody>
  26. </table>
  27. </div>
  28. </div>
  29. </body>
  30. </html>
  31.  
  32. <script src="js/script.js"></script>

Creating the Script

This code contains the script of the application. This code will dynamically display table of multiplication when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function displayTable(){
  2. var number = document.getElementById('number').value;
  3.  
  4.  
  5. if(number == ""){
  6. alert("Enter some number first");
  7. }else{
  8. var digit = 0;
  9. if(number <= 0){
  10. digit = 1;
  11. document.getElementById('number').value = 1;
  12. }else if(number > 10){
  13. digit = 10;
  14. document.getElementById('number').value = 10;
  15. }else{
  16. digit = number;
  17. }
  18.  
  19.  
  20.  
  21. var data = "";
  22. for(var i = 1; i <= digit; i++) {
  23. data += "<tr class='alert-info'>";
  24. for(var j = 1; j <= 10; j++){
  25.  
  26. data += "<td>"+i*j+"</td>";
  27. }
  28. data += "</tr>";
  29. }
  30.  
  31. document.getElementById('data').innerHTML = data;
  32. }
  33. }
  34.  
  35. document.getElementById("number").onkeyup=function(){
  36. var input=parseInt(this.value);
  37.  
  38. if(input > 10){
  39. this.value = 10;
  40. }
  41. if(input < 0){
  42. this.value = 1;
  43. }
  44.  
  45. }
There you have it we successfully created a Generate 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