diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..89ea4925 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,20 +1,14 @@ - + - - + Book library + + - - + + @@ -23,51 +17,28 @@

Library

Add books to your virtual library

-
-
+
- - - + + + + + - - - -
+ + +
+ + +
+ + +
@@ -77,7 +48,7 @@

Library

- + @@ -93,4 +64,4 @@

Library

- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..80282a69 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,103 +1,103 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.onload = function () { + // Cache DOM elements with clear suffixes + const titleInputEl = document.getElementById("title"); + const authorInputEl = document.getElementById("author"); + const pagesInputEl = document.getElementById("pages"); + const readCheckEl = document.getElementById("check"); + const submitBtnEl = document.getElementById("submitBtn"); + const bookFormEl = document.getElementById("bookForm"); + const tbodyEl = document.querySelector("#display tbody"); + + // Initial setup 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(); + // Event listeners + submitBtnEl.addEventListener("click", addBook); + + // --- Functions --- + 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, book2); + render(); + } } -} -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); + function addBook() { + // Preprocess & validate input + const title = titleInputEl.value.trim(); + const author = authorInputEl.value.trim(); + const pages = pagesInputEl.value.trim(); + const read = readCheckEl.checked; -//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(); - } + if (!title || !author || !pages || isNaN(Number(pages)) || Number(pages) <= 0) { + alert("Please fill all fields with valid values (no empty spaces, pages must be a positive number)."); + return; + } + if (!title || title.length < 2) { + alert("Title must be at least 2 characters long."); + return; } - -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; +if (!author || author.length < 2) { + alert("Author must be at least 2 characters long."); + return; +} +const pageNum = Number(pages); +if (!Number.isInteger(pageNum) || pageNum <= 0 || pageNum > 10000) { + alert("Pages must be a positive whole number (1–10,000)."); + return; } -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); + // Add book + let book = new Book(title, author, pages, read); + myLibrary.push(book); + render(); + bookFormEl.reset(); } - //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; + function Book(title, author, pages, read) { + this.title = title; + this.author = author; + this.pages = pages; + this.read = read; + } - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; - render(); - }); + function render() { + tbodyEl.innerHTML = ""; - //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); - render(); + myLibrary.forEach((book, i) => { + let row = tbodyEl.insertRow(); + + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; + + // read/unread button + let wasReadCell = row.insertCell(3); + let changeBtnEl = document.createElement("button"); + changeBtnEl.className = "btn btn-success"; + changeBtnEl.textContent = book.read ? "Yes" : "No"; + changeBtnEl.addEventListener("click", () => { + book.read = !book.read; + render(); + }); + wasReadCell.appendChild(changeBtnEl); + + // delete button + let deleteCell = row.insertCell(4); + let delBtnEl = document.createElement("button"); + delBtnEl.className = "delete-btn"; + delBtnEl.textContent = "Delete"; + delBtnEl.addEventListener("click", () => { + myLibrary.splice(i, 1); + render(); + alert(`The book "${book.title}" has been deleted successfully.`); + }); + deleteCell.appendChild(delBtnEl); }); } -} +}; diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..131f6607 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,114 @@ -.form-group { - width: 400px; - height: 300px; - align-self: left; - padding-left: 20px; +/* General page styling */ +body { + background: #f7f9fc; + font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; + color: #333; + margin: 0; + padding: 0; } -.btn { - display: block; +/* Jumbotron header */ +.jumbotron { + background: linear-gradient(135deg, #4facfe, #00f2fe); + color: white; + padding: 3rem 2rem; + border-radius: 0 0 20px 20px; + box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15); } -.form-check-label { - padding-left: 20px; - margin: 5px 0px 5px 0px; +/* Add Book button */ +button.btn-info { + margin: 1rem 0; + background: #03cceb; + border: none; + width: 15%; + height: 30px; + transition: background 0.3s ease, transform 0.2s ease; } -button.btn-info { - margin: 20px; +button.btn-info:hover { + background: #4facfe; + transform: translateY(-2px); +} + +/* Form styling */ +#demo { + background: #fff; + padding: 1.5rem; + margin: 1rem 0; + border-radius: 12px; + box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); +} + +#bookForm label { + font-weight: 600; + margin-top: 0.5rem; +} + +#bookForm input[type="text"], +#bookForm input[type="number"] { + border-radius: 8px; + border: 1px solid #ccc; + padding: 0.5rem; + margin-bottom: 0.8rem; +} + +#bookForm input:focus { + border-color: #17a2b8; + box-shadow: 0 0 5px rgba(23, 162, 184, 0.4); +} + +#submitBtn { + width: 10%; + border-radius: 8px; + padding: 0.6rem; + font-weight: 600; +} + +/* Table styling */ +.table { + background: #fff; + border-radius: 12px; + overflow: hidden; + margin-top: 2rem; + box-shadow: 0 3px 12px rgba(0, 0, 0, 0.08); +} + +.table th { + background: #343a40 !important; + color: #fff; + text-align: center; + vertical-align: middle; +} + +.table td { + text-align: center; + vertical-align: middle; +} + +/* Delete button */ +.delete-btn { + background: #e74c3c; + border: none; + color: white; + padding: 6px 12px; + border-radius: 6px; + cursor: pointer; + font-size: 0.85rem; + transition: background 0.3s ease; +} + +.delete-btn:hover { + background: #c0392b; +} + +/* Checkbox styling */ +.form-check-input { + transform: scale(1.2); + margin-right: 8px; +} + +/* Smooth transitions */ +* { + transition: all 0.2s ease-in-out; }
Author Number of Pages ReadAction