Table of 2 in javascript using for loop

Example 1: Multiplication Table Up to 10

// program to generate a multiplication table

// take input from the user
const number = parseInt(prompt('Enter an integer: '));

//creating a multiplication table
for(let i = 1; i <= 10; i++) {

    // multiply i with number
    const result = i * number;

    // display the result
    console.log(`${number} * ${i} = ${result}`);
}

Output

Enter an integer: 3
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30

In the above program, the user is prompted to enter an integer value. Then, the for loop is used to iterate through 1 to 10 to create a multiplication table.


Example 2: Multiplication Table Up to a Range

/* program to generate a multiplication table
upto a range */

// take number input from the user
const number = parseInt(prompt('Enter an integer: '));

// take range input from the user
const range = parseInt(prompt('Enter a range: '));

//creating a multiplication table
for(let i = 1; i <= range; i++) {
    const result = i * number;
    console.log(`${number} * ${i} = ${result}`);
}

Output

Enter an integer: 7
Enter a range: 5
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

In the above example, the user is prompted to enter an integer and also a range for which they want to create a multiplication table.

The user enters an integer (here 7) and a range (here 5). Then a multiplication table is created using a for loop for that range.

I am trying to create a simple times table in a html document using Javascript. This is my code so far:


    
    
        Table
    
    
    
    
    

I looked through the posted questions, but could not find an answer, possibly because I am a beginner programmer and did not understand most of it.

Table of 2 in javascript using for loop

asked Feb 26, 2014 at 12:58

2

Well, first of all you should insert the tr (rows) and td (cells) into a table element... Something like

document.write("");
// your loop here
document.write("
");

There are better ways to do this, though!

answered Feb 26, 2014 at 13:04

Table of 2 in javascript using for loop

LucaLuca

1,0501 gold badge14 silver badges24 bronze badges

1

use '+' to concate.




Table





answered Feb 26, 2014 at 13:00

TKVTKV

2,36310 gold badges42 silver badges55 bronze badges







answered Aug 31, 2015 at 18:04

Table of 2 in javascript using for loop







answered Aug 31, 2015 at 18:00

Table of 2 in javascript using for loop

function year(){ var test = ''; var tr=''; for(var i=0;i<4;i++){ tr += ''; for(var j=0;j<4;j++){ tr += ''; } } tr +=''; test += tr; return document.getElementById('yr').innerHTML = test; } year();

Noy

1,2482 gold badges11 silver badges28 bronze badges

answered Feb 16, 2016 at 11:05

Table of 2 in javascript using for loop

vijayvijay

1862 silver badges12 bronze badges

1







<2015-2016>
'+2015+'

answered Oct 13, 2017 at 10:12

How do you write two tables in a for loop?

“how to make a for loop do the 2 times table in python” Code Answer's.
a=int(input("enter table number")).
b=int(input("enter the number to which table is to printed")).
while i<=b:.
print(a,"x",i,"=",a*i).
i=i+1..

How do you print a table in JavaScript?

printWindow. document. write('Table Contents'); //Print the Table CSS.