diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..d01cf980 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,8 @@ - - + + - - + Book Library + @@ -30,42 +26,25 @@

Library

- + - + - +
@@ -91,6 +70,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..05da1931 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,8 +1,14 @@ -let myLibrary = []; +const myLibrary = []; + +const titleInput = document.getElementById("title"); +const authorElement = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkOption = document.getElementById("check"); window.addEventListener("load", function (e) { populateStorage(); - render(); + const submitInput = document.getElementById("submit"); + submitInput.addEventListener("click", submit); }); function populateStorage() { @@ -20,41 +26,43 @@ function populateStorage() { } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); - //check the right input from forms and if its ok -> add the new book (object in array) //via Book function and start render function function submit() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + const title = titleInput.value.trim(); + const author = authorElement.value.trim(); + const pages = Number(pagesInput.value.trim()); + const isChecked = checkOption.checked; + + if (!title || !author || Number.isNaN(pages) || pages <= 0) { + alert( + "Please fill all fields and make sure book page is greater than zero!" + ); + return; } + + if (!/^[A-Za-z\s]+$/.test(author) || !/^[A-Za-z\s]+$/.test(title)) { + alert("Author and book title must contain only letters"); + return; + } + + let book = new Book(title, author, pages, isChecked); + myLibrary.push(book); + render(); } -function Book(title, author, pages, check) { +function Book(title, author, pages, isChecked) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.isChecked = isChecked; } function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + for (let n = rowsNumber - 1; n >= 1; n--) { table.deleteRow(n); } //insert updated row and cells @@ -70,32 +78,28 @@ function render() { authorCell.innerHTML = myLibrary[i].author; pagesCell.innerHTML = myLibrary[i].pages; - //add and wait for action for read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; + let readStatusBtn = document.createElement("button"); + readStatusBtn.className = "btn btn-success"; + wasReadCell.appendChild(readStatusBtn); + readStatusBtn.innerText = myLibrary[i].isChecked ? "Yes" : "No"; - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + readStatusBtn.addEventListener("click", function () { + myLibrary[i].isChecked = !myLibrary[i].isChecked; render(); }); //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + + let removeBookBtn = document.createElement("button"); + + deleteCell.appendChild(removeBookBtn); + removeBookBtn.className = "btn btn-warning"; + removeBookBtn.innerHTML = "Delete"; + removeBookBtn.addEventListener("click", function () { + const toast = document.createElement("div"); + toast.textContent = `You've deleted title: ${myLibrary[i].title}`; + table.appendChild(toast); + setTimeout(() => toast.remove(), 2000); myLibrary.splice(i, 1); render(); });