From c1cd76ecb0f9d0f2df7be195409caa407961eef2 Mon Sep 17 00:00:00 2001 From: Facundo Saucedo <43495919@terciariourquiza.edu.ar> Date: Mon, 1 Jun 2026 21:13:47 -0300 Subject: [PATCH] feat: add validation for book input and alert on invalid inputs --- script.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/script.js b/script.js index 0b4003f..77ab5d2 100644 --- a/script.js +++ b/script.js @@ -33,5 +33,36 @@ const renderTable = (books) => { } } +const validateForm = (tittle, author, year, score) => { + if (!tittle || !author || !year || !score) return false; + if (year < 0 || score < 0 || score > 10) return false; + return true; +} + +const addBook = (tittle, author, year, score) => { + if(!validateForm(tittle, author, year, score)) { + alert('Datos invalidos'); + return; + } + + books.push({tittle: tittle, year: Number(year), score: Number(score), author: author}); + renderTable(books); +} + +document.getElementById('boton-agregar').addEventListener('click', () => { + + const tittle = document.getElementById('titulo').value; + const author = document.getElementById('autor').value; + const year = document.getElementById('anio').value; + const score = document.getElementById('calificacion').value; + + addBook(tittle, author, year, score); + + document.getElementById('titulo').value = ""; + document.getElementById('autor').value = ""; + document.getElementById('anio').value = ""; + document.getElementById('calificacion').value = ""; +}) + renderTable(books); \ No newline at end of file