Download Source Code -JavaScript – Generate Table Of Multiplication

Loading...

JavaScript – Generate Table Of Multiplication

Submitted by: 
razormist
Language: 
JavaScript
Visitors have accessed this post 5 times.

In this tutorial we will create a Generate Table Of Multiplication using JavaScript. This code will automatically 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 nested for() loops in order to loop through into 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. <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="<a href="https://sourcecodester.com"" rel="nofollow">https://sourcecodester.com"</a> 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">JavaScript - Generate Table Of Multiplication</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="input"/>
  21. <br />
  22. <center><button onclick="generateTable()" class="btn btn-primary">Generate</button></center>
  23. </div>
  24. </div>
  25. <div class="col-md-9">
  26. <div>
  27. <table class="table table-bordered">
  28. <thead class="alert-info">
  29. <tr>
  30. <th colspan="10"><center>Table of Multiplication</center></th>
  31. </tr>
  32. </thead>
  33. <tbody id="result"></tbody>
  34. </table>
  35. </div>
  36. </div>
  37. </body>
  38. </html>
  39.  
  40. <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 generateTable(){
  2. var input = document.getElementById('input').value;
  3.  
  4. if(input == ""){
  5. alert("Enter some number first");
  6. }else{
  7. var digit = 0;
  8. if(input <= 0){
  9. digit = 1;
  10. document.getElementById('input').value = 1;
  11. }else{
  12. digit = input;
  13. }
  14.  
  15.  
  16.  
  17. var html = "";
  18. for(var i = 1; i <= digit; i++) {
  19. html += "<tr>";
  20. for(var j = 1; j <= 10; j++){
  21.  
  22. html += "<td>"+i*j+"</td>";
  23. }
  24. html += "</tr>";
  25. }
  26.  
  27. document.getElementById('result').innerHTML = html;
  28. document.getElementById('result').setAttribute('class', 'alert-warning');
  29. }
  30. }

There you have it we successfully created a Generate Table Of Multiplication 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!

Tags: 
Javascript
table
Bootstrap

Add new comment

(Visited 2 times, 1 visits today)
Loading...