Exercise 18: Identifying Vowels with a Switch Statement
Discover how to detect vowels in a text string using a switch statement. An ideal exercise to reinforce your knowledge of conditional statements.
The main purpose is to practice using a switch statement.switchto identify if a character is a vowel.
Activity
-
Ask the user to enter a letter.
-
Use
switchto check if the entered letter is a vowel (a,e,i,o,u(in lowercase and uppercase). -
Display a message in the console indicating whether the letter is a vowel or not.
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>Identificación de Vocales con switch</title>
</head>
<body>
<h1>Ejercicio 18: Identificación de Vocales con switch</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 letra = prompt("Ingresa una letra:");
switch (letra.toLowerCase()) {
case "a":
case "e":
case "i":
case "o":
case "u":
console.log("Es una vocal.");
break;
default:
console.log("No es una vocal.");
}
info
🔎Check the sectionControl instructions in JavaScriptwhere you will find more information on this topic.