diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..b1cf83e3 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,3 +1,71 @@ + + + + + Personal Library Manager + + + + + + + + + + + + + +
+

Library

+

Add books to your virtual library

+
+ +
+ + +
+
+ + + + + + + + + +
+ + +
+ + +
+
+ + + + + + + + + + + + + +
TitleAuthorPagesRead StatusActions
+
+ + + + + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..4143f326 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,47 +1,21 @@ -let myLibrary = []; +const myLibrary = []; -window.addEventListener("load", function (e) { +// DOM Element Selectors +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); +const submitBtn = document.getElementById("submitBtn"); +const tableBody = document.getElementById("tableBody"); + +// Initialize app +window.addEventListener("load", () => { populateStorage(); render(); + submitBtn.addEventListener("click", submitBook); }); -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(); - } -} -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(); - } -} function Book(title, author, pages, check) { this.title = title; @@ -50,54 +24,82 @@ function Book(title, author, pages, check) { this.check = check; } -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); +function populateStorage() { + if (myLibrary.length === 0) { + // Note: pages are stored as numbers + const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); + myLibrary.push(book1, book2); } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; 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; - - //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; - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; +} + +function submitBook() { + // Input Validation & Normalization + const titleVal = titleInput.value.trim(); + const authorVal = authorInput.value.trim(); + const pagesVal = parseInt(pagesInput.value); + + // Reject empty strings or invalid page numbers + if (!titleVal || !authorVal || isNaN(pagesVal) || pagesVal <= 0) { + alert("Please provide a valid Title, Author, and Page Count."); + return; + } + + const newBook = new Book(titleVal, authorVal, pagesVal, checkInput.checked); + myLibrary.push(newBook); + + // Clear inputs + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + + render(); +} + + +function render() { + tableBody.innerHTML = ""; + + myLibrary.forEach((book, index) => { + // You can also use row = tableBody.insertRow() here! + const row = document.createElement("tr"); + + // Title Cell (Index 0) + const titleCell = row.insertCell(0); + titleCell.textContent = book.title; + + // Author Cell (Index 1) + const authorCell = row.insertCell(1); + authorCell.textContent = book.author; + + // Pages Cell (Index 2) + const pagesCell = row.insertCell(2); + pagesCell.textContent = book.pages; + + // Read Status Cell (Index 3) + const statusCell = row.insertCell(3); + const statusBtn = document.createElement("button"); + statusBtn.className = "btn btn-sm btn-success"; + statusBtn.textContent = book.check ? "Yes" : "No"; + statusBtn.addEventListener("click", () => { + book.check = !book.check; render(); }); + statusCell.appendChild(statusBtn); - //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}`); - myLibrary.splice(i, 1); + // Delete Cell (Index 4) + const deleteCell = row.insertCell(4); + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-sm btn-warning"; + deleteBtn.textContent = "Delete"; + deleteBtn.addEventListener("click", () => { + myLibrary.splice(index, 1); render(); + alert(`Deleted: ${book.title}`); }); - } + deleteCell.appendChild(deleteBtn); + + tableBody.appendChild(row); + }); }