By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript Program > How to Print The Table of Any User Defined Number Using Function
JavaScript Program

How to Print The Table of Any User Defined Number Using Function

Rocoderes
Last updated: 2022/09/22 at 4:15 AM
Rocoderes
Share
3 Min Read
Print The Table

In this article, we will make a program to print the table of any user defined number using a function. Here, we will get a number from the user, and we will just print the whole table. We will add the logic for this table in a function. We will use some HTML element to get the user’s input, but we can also use prompt instead of input field to make this program in JavaScript purely.

Contents
Creating Program Using HTML and JSOutputYou may also like:

We will create a function in which we will print the table, like we do in mathematics. Here we will take user input, then output should be a table of that number. For example, user inputs the number 5 than output should be 5,10,15,20,25,30,35,40,45,50. This will be so easy program to do, so let’s make it.

Creating Program Using HTML and JS

So firstly, we will make an HTML markup as always. Then in this we have to add a script tag to write the JS code. You can also make a separate file for the JS and add the path in the HTML. In this HTML, we have added an input field to get the user’s input and a button, on which we have applied onclick event listener with a function call to get the result.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="number" id="num1">
    <button onclick= res()>Result</button>
    <script>
        var result;
        function res(){
            var x = document.querySelector("input").value;
            for(var i=1; i<=10; i++){
                result = i * x;
                document.write(`<h2>${x} X ${i} = ${result}</h2>`) 
            }
        }
    </script>
</body>
</html>

Now, in this function, we have declared a variable x in which we have to fetch the value using document.querySelector("num1") to get value from the input value. Then we have added another variable in which we will store the value for the result. In this, we will apply the logic to get table. For that we have added a for loop which will starts from 1 value, and it will cycle for 10 times. In this loop, we will just multiply the i with the inputted value and store in the result. Once the loop gets completed, then we print the result.

If you want to make it in JS only, then you can remove input field and button. And you can use prompt() to get the user’s value.

Output

Print The Table

You may also like:

  • How to Check If a Number is Positive, Negative, or Zero
  • How to Convert Kilometers to Miles and Celsius to Fahrenheit
  • How to Add Two Numbers, Swap Two Numbers And Find Square Root in JavaScript
  • Write a program to Check if a Number is Odd or Even in JavaScript

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Print The Table, programs
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Rocoderes
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Largest Among Three Numbers How to Find the Largest Among Three Numbers
Next Article Armstrong Number How to Find an Armstrong Number Using JavaScript
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Computing Fibonacci Sequence in JavaScript

Computing Fibonacci Sequence in JavaScript

January 12, 2023
Swap Two Numbers Without A Third Variable

How to Swap Two Numbers Without A Third Variable in JS

December 12, 2022
Armstrong Number

How to Find an Armstrong Number Using JavaScript

September 23, 2022
Largest Among Three Numbers

How to Find the Largest Among Three Numbers

September 21, 2022
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?