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