diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..05eac349 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,11 @@ - + + - - + + + Library - Add and View Books + @@ -20,7 +19,7 @@

Library

-

Add books to your virtual library

+

Add books to your virtual library..

-
+
Library /> Library class="btn btn-primary" onclick="submit();" /> -
+
@@ -91,6 +90,6 @@

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..dd8b27a6 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -17,6 +17,7 @@ function populateStorage() { myLibrary.push(book1); myLibrary.push(book2); render(); + } } @@ -29,19 +30,29 @@ const check = document.getElementById("check"); //via Book function and start render function function submit() { if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + + title.value.trim() === "" || + author.value.trim() === "" || + pages.value.trim() === "" ) { alert("Please fill all fields!"); return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + } + const pagesNumber = Number(pages.value.trim()); + if (isNaN(pagesNumber) || pagesNumber <= 0) { + alert("Please enter a valid number of pages."); + return false; + } + + let book = new Book(title.value, author.value, pagesNumber, check.checked); + myLibrary.push(book); render(); + } -} + + + + function Book(title, author, pages, check) { this.title = title; @@ -54,9 +65,14 @@ function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { - table.deleteRow(n); - } + + table.innerHTML = table.rows[0].outerHTML; + // for (let n = rowsNumber - 1; n > 0; n-- ) { + // table.deleteRow(n); + // } + + myLibrary.sort((a, b) => a.title.localeCompare(b.title)); + //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { @@ -66,35 +82,28 @@ function render() { let pagesCell = row.insertCell(2); let wasReadCell = row.insertCell(3); let deleteCell = row.insertCell(4); - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = 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; + //add and wait for action for read/unread button. + let statusButton = document.createElement("button"); + statusButton.className = "btn btn-success"; + wasReadCell.appendChild(statusButton); + let readStatus = myLibrary[i].check ? "Read" : "Unread"; + statusButton.innerText = readStatus; - changeBut.addEventListener("click", function () { + statusButton.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; 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 () { + let deleteButton = document.createElement("button"); + deleteCell.appendChild(deleteButton); + deleteButton.className = "btn btn-warning"; + deleteButton.innerHTML = "Delete"; + deleteButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render();