Skip to content

J020 Loop

A basic task focusing on for-loops in ES (ECMAScript).

Create an empty folder and within it, create a file named app.js. In this file, construct a multiplication table that, when printed to the console, looks approximately like this:

    1    2    3    4    5    6    7    8    9   10
    2    4    6    8   10   12   14   16   18   20
    3    6    9   12   15   18   21   24   27   30
    ... (and so on up to 10)

Tip

Consider using two loops and the padStart(5) method (available on the String type) to right-align numbers in the table.

Solution
"use strict";

for (let i = 1; i <= 10; i++) {
    let row = "";
    for (let j = 1; j <= 10; j++) {
        row += (i * j).toString().padStart(5);
    }
    console.log(row);
}