1 Commits

Author SHA1 Message Date
Juanse Marquez
5f7e970258 Solución ejercicio 1 2026-06-01 18:41:31 -03:00
4 changed files with 26 additions and 62 deletions

View File

@@ -1,21 +1,14 @@
// Agregar aquí el código javascript const boton = document.querySelector("#boton");
// 1. Selección de los elementos del DOM
const inputNombre = document.querySelector("#campo-nombre");
const boton = document.querySelector("#boton-saludo");
const resultado = document.querySelector("#mensaje-resultado");
// 2. Registro del Event Listener utilizando una función clásica boton.addEventListener("click", () => {
boton.addEventListener("click", function () { const nombre = document.querySelector("#nombre").value;
// 3. Obtención y limpieza del valor del campo de texto const saludo = document.querySelector("#saludo");
const nombre = inputNombre.value.trim(); // trim() elimina espacios al inicio y al final, para que un texto
// con solo espacios no se considere válido.
// 4. Estructura condicional para validar el contenido if (nombre.trim() === "") {
if (nombre === "") { saludo.textContent = "Por favor, ingresá tu nombre.";
// Caso en que el campo esté vacío
resultado.textContent = "Por favor, ingresá tu nombre.";
} else { } else {
// Caso en que contenga texto válido saludo.textContent = `Hola, ${nombre}!`;
resultado.textContent = `Hola, ${nombre}!`;
} }
}); });

View File

@@ -8,9 +8,10 @@
</head> </head>
<body> <body>
<h1>Ejercicio 1</h1> <h1>Ejercicio 1</h1>
<input type="text" id="campo-nombre" placeholder="Ingresá tu nombre"> <input type="text" id="nombre" placeholder="Tu nombre">
<button id="boton-saludo">Saludar</button> <button id="boton">Saludar</button>
<p id="mensaje-resultado"></p> <p id="saludo"></p>
<script src="ejercicio1.js"></script> <script src="ejercicio1.js"></script>
</body> </body>
</html> </html>

View File

@@ -1,29 +1 @@
// Agregar aquí el código javascript // Agregar aquí el código javascript
// 1. Selección de los elementos del DOM mediante sus identificadores
const inputItem = document.getElementById("input-item");
const btnAgregar = document.getElementById("btn-agregar");
const listaContenedor = document.getElementById("lista-contenedor");
// 2. Registro del escuchador de eventos para el clic del botón
btnAgregar.addEventListener("click", function () {
// 3. Captura y limpieza de espacios en blanco del valor actual del input
const textoItem = inputItem.value.trim();
// 4. Validación: si la cadena está vacía, se interrumpe la ejecución
if (textoItem === "") {
return;
}
// 5. Creación de un nuevo elemento de lista (<li>) en memoria
const nuevoLi = document.createElement("li");
// 6. Asignación del texto validado al contenido del nuevo elemento
nuevoLi.textContent = textoItem;
// 7. Inserción del nuevo elemento como hijo del contenedor <ul>
listaContenedor.appendChild(nuevoLi);
// 8. Restablecimiento del campo de texto a una cadena vacía
inputItem.value = "";
});

View File

@@ -1,16 +1,14 @@
<!doctype html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8">
<meta name="viewport" content="width=device-width" /> <meta name="viewport" content="width=device-width">
<title>Ejercicio 2</title> <title>Ejercicio 2</title>
<link rel="stylesheet" href="estilo.css" /> <link rel="stylesheet" href="estilo.css">
</head> </head>
<body> <body>
<h1>Ejercicio 2</h1> <h1>Ejercicio 2</h1>
<input type="text" id="input-item" placeholder="Escribe un nuevo ítem" />
<button id="btn-agregar">Agregar</button> <script src="ejercicio2.js"></script>
<ul id="lista-contenedor"></ul> </body>
<script src="ejercicio2.js"></script>
</body>
</html> </html>