Javascript Multiplication Table

This is a preview lesson
Register or sign in to take this lesson.

Multiplication Table

This tutorial shows a snippet for displaying multiplication table.

Pseudo-code

number = 10
iterator = 1
endValue = 10

while( iterator  less than equals  endValue) {
    print(number + " X " + iterator + " = " + ( number * iterator ) );
    iterator++;
}

/*
* OUTPUT:
* 10 X 1 = 10
* 10 X 2 = 20
* ...  The output is printed in the above format till the end value of 10
*/

JavaScript Syntax

let multiplicationValue = number * iterator;        // gives out multiplication of a number

Let’s look at multiplication table with constant assigned.

const number = 10
const iterator = 1
const endValue = 10;

while( iterator <= 10) {
    console.log(number + " X " + iterator + " = " + ( number * iterator ) );
    iterator++;
}

/* 
* OUTPUT:
* 10 X 1 = 10
* 10 X 2 = 20
* ...  The output is printed in the above format till the end value of 10.
*/

Now the the same code is wrapped around a function, making it possible to be dynamic.

// Function that prints out the multiplication table
function getMultiplicationTable(number, endValue) {
    const iterator = 1;
    while( iterator <= 10) {
        console.log(number + " X " + iterator + " = " + ( number * iterator ) );
        iterator++;
    }
}

getMultiplicationTable(10, 2);
/* 
* OUTPUT:
* 10 X 1 = 10
* 10 X 2 = 20
* The output is printed in the above format till the end value of 2.
*/

getMultiplicationTable(2, 5);
/* 
* OUTPUT:
* 2 X 1 = 2
* 2 X 2 = 4
* 2 X 3 = 6
* 2 X 4 = 8
* 2 X 5 = 10
* The output is printed in the above format till the end value of 5.
*/