Skip to main content

Exercise 20: Generate a multiplication table using a while loop

Develop a function that generates multiplication tables using loops. Ideal for practicing loops and repetitive structures in JavaScript.

The main goal is to learn how to use loops.whileTo generate the multiplication table for a number entered by the user.

Activity

  1. Ask the user to enter a number.

  2. Use a loopwhileto show the multiplication table of that number from1 until 103. Display each result in the console.

Solution

Step 1: Create the index.html file

HTML
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Generar tabla de multiplicar con while</title>
</head>
<body>
<h1>Ejercicio 20: Generar tabla de multiplicar con while</h1>
<p>Los cambios se muestran en consola.</p>
<script src="script.js"></script>
</body>
</html>

Step 2: Write the code in script.js

JavaScript
let numero = parseInt(
prompt("Ingresa un número para ver su tabla de multiplicar:"),
);
let contador = 1;

while (contador <= 10) {
console.log(`${numero} x ${contador} = ${numero * contador}`);
contador++;
}
info

🔎Check the sectionControl instructions in JavaScriptwhere you will find more information on this topic.