Exercise 15: Age Classification with JavaScript
Create a function that classifies ages according to ranges. Learn conditional logic and improve your decision-making skills with JavaScript.
The goal is to practice control structures.if/elseto make decisions based on the entered age.
Activity
-
Ask the user to enter their age.
-
Use
if/elseTo classify age into the following categories:
- Under 13 years old: "Child"
- Between 13 and 18 years old: "Teen"
- Over 18 years old: "Adult"
- Display the corresponding age category 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>Clasificación de Edad</title>
</head>
<body>
<h1>Ejercicio 15: Clasificación de Edad</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 edad = prompt("Ingresa tu edad:");
if (edad < 13) {
console.log("Niño");
} else if (edad <= 18) {
console.log("Adolescente");
} else {
console.log("Adulto");
}
info
🔎Check the sectionControl instructions in JavaScriptwhere you will find more information on this topic.