diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..031d65f0 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - - + Book Library + + @@ -23,51 +20,55 @@

Library

Add books to your virtual library

-
-
- - - - - - -
+
@@ -81,16 +82,10 @@

Library

- - - - - - - +
- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..b6c5ba63 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,15 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); + if (myLibrary.length === 0) { + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); // Changed "252" to 252 + let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); // Changed "127" to 127 + myLibrary.push(book1, book2); } } @@ -25,79 +18,78 @@ 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 == "" - ) { +// Add a new book to the library +function addBook() { + const titleValue = title.value.trim(); + const authorValue = author.value.trim(); + const pagesValue = pages.value.trim(); + const isRead = check.checked; + + // Validate input fields + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill all fields!"); return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); } -} -function Book(title, author, pages, check) { + // Validate "number of pages" field + const pagesNumber = parseInt(pagesValue, 10); + if (isNaN(pagesNumber) || pagesNumber <= 0) { + alert("Please enter a valid number of pages!"); + return false; + } + + // Create a new book and add it to the library + const book = new Book(titleValue, authorValue, pagesNumber, isRead); + myLibrary.push(book); + render(); +} +// Book constructor +function Book(title, author, pages, read) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.check = read; } +// Render the library table function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + + // Clear existing rows (except the header) + for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { + + // Populate the table with updated library data + myLibrary.forEach((book, i) => { let row = table.insertRow(1); - let titleCell = row.insertCell(0); - let authorCell = row.insertCell(1); - 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; + row.insertCell(0).innerText = book.title; + row.insertCell(1).innerText = book.author; + row.insertCell(2).innerText = book.pages; - //add and wait for action for read/unread button + // Add read/unread toggle button + let wasReadCell = row.insertCell(3); 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; - + changeBut.innerText = book.check ? "Yes" : "No"; changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + book.check = !book.check; render(); }); + wasReadCell.appendChild(changeBut); - //add delete button to every row and render again + // Add delete button + let deleteCell = row.insertCell(4); 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}`); - myLibrary.splice(i, 1); - render(); + delButton.className = "btn btn-primary"; + delButton.textContent = "Delete"; + delButton.addEventListener("click", function () { + if (confirm(`Delete "${book.title}" from your library?`)) { + myLibrary.splice(i, 1); + render(); + } }); - } -} + deleteCell.appendChild(delButton); + }); +} \ No newline at end of file