diff --git a/index.html b/index.html index c975a59..2709e15 100644 --- a/index.html +++ b/index.html @@ -31,6 +31,7 @@

+

diff --git a/script.js b/script.js index 234668b..67f9bfb 100644 --- a/script.js +++ b/script.js @@ -7,8 +7,71 @@ let libros = [ function mostrarTabla(datos) { // Escribir esta función para resolver el punto 1. + const libreria = document.querySelector("#listado_libros"); + + for (const libro of libros){ + const fila = document.createElement("tr"); + fila.innerHTML = ` + ${libro.titulo} + ${libro.autor} + ${libro.anio} + ${libro.puntaje} + `; + libreria.appendChild(fila); + } } - - // Invocamos la función al inicio para poblar la tabla con los datos del array mostrarTabla(libros); + +const form = document.querySelector("#boton-agregar") + +function taMal(id, mensaje) { + document.querySelector(id).textContent = mensaje; +} + +function yaNoTa(id) { + document.querySelector(id).textContent = "" +} + +form.addEventListener("click", (e) => { + e.preventDefault(); + + const titulo = document.querySelector("#titulo").value.trim(); + const autor = document.querySelector("#autor").value.trim(); + const anio = document.querySelector("#anio").value.trim(); + const calificacion = document.querySelector("#calificacion").value; + + yaNoTa("#error"); + yaNoTa("#error"); + yaNoTa("#error"); + yaNoTa("#error"); + + + let valido = true; + + + if (titulo === "") { + taMal("#error", "El titulo no puede estar vacío."); + valido = false; + } + + if (autor === "") { + taMal("#error", "El autor no puede estar vacío."); + valido = false; + } + + if (anio === "") { + taMal("#error", "El año no puede estar vacío."); + valido = false; + } + + if (calificacion === "" || parseInt(calificacion) < 0 || parseInt(calificacion) > 10) { + taMal("#error", "La calificaion tiene que ser mayor que 0 y menor que 10."); + valido = false; + } + + if (valido) { + document.querySelector("#exito").textContent = "Formulario enviado correctamente."; + form.reset(); // reset() limpia todos los campos del formulario + } +}); \ No newline at end of file