From ab926da5b10ff601a65aca1d6e7711281b71f4d0 Mon Sep 17 00:00:00 2001 From: TTiamiyu Date: Tue, 4 Aug 2026 00:05:53 +0100 Subject: [PATCH 1/5] Refactor HTML structure for improved readability and maintainability --- debugging/book-library/index.html | 144 ++++++++++++------------------ 1 file changed, 56 insertions(+), 88 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..4b88b040 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,64 @@ - - - - - - - - - - -
-

Library

-

Add books to your virtual library

-
+ + + + + + + + + + + +
+

Library

+

Add books to your virtual library

+
- + -
-
- - - - - - - - -
+
+
+ + + + + + + + + />
+
+ + + + + + + + + + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + - - - + \ No newline at end of file From 504b1d2df421156677bd2b86fafa51c91f907c5b Mon Sep 17 00:00:00 2001 From: TTiamiyu Date: Tue, 4 Aug 2026 00:06:10 +0100 Subject: [PATCH 2/5] Fix input validation and improve render function logic --- debugging/book-library/script.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..efecc4d2 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -16,7 +16,6 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); } } @@ -28,12 +27,7 @@ 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 == "" - ) { + if (!title.value || !author.value || !pages.value) { alert("Please fill all fields!"); return false; } else { @@ -54,7 +48,7 @@ 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 > 0; n--) { table.deleteRow(n); } //insert updated row and cells @@ -76,7 +70,7 @@ function render() { changeBut.className = "btn btn-success"; wasReadCell.appendChild(changeBut); let readStatus = ""; - if (myLibrary[i].check == false) { + if (myLibrary[i].check == true) { readStatus = "Yes"; } else { readStatus = "No"; @@ -89,12 +83,12 @@ function render() { }); //add delete button to every row and render again - let delButton = document.createElement("button"); + let delBut = document.createElement("button"); delBut.id = i + 5; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From 312865bb484ee52c9846451165868307193e73b2 Mon Sep 17 00:00:00 2001 From: TTiamiyu Date: Tue, 4 Aug 2026 00:18:42 +0100 Subject: [PATCH 3/5] Fix book submission logic by correcting author assignment and clearing input fields --- debugging/book-library/script.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index efecc4d2..1de7c0c1 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -31,12 +31,18 @@ function submit() { alert("Please fill all fields!"); return false; } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); + + // Add these lines to clear the text boxes after adding: + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + render(); } } - function Book(title, author, pages, check) { this.title = title; this.author = author; From 7aa8476f5db53f248971c00b19810154053bb83b Mon Sep 17 00:00:00 2001 From: TTiamiyu Date: Wed, 5 Aug 2026 05:51:04 +0100 Subject: [PATCH 4/5] Refactor HTML structure and improve form handling for book submissions, fixed errors in HTML. --- debugging/book-library/index.html | 104 +++++++++++++++++------------- debugging/book-library/script.js | 53 ++++++++------- debugging/book-library/style.css | 12 ++-- 3 files changed, 90 insertions(+), 79 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 4b88b040..31a191b8 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,62 +1,74 @@ - + - - + + + Virtual Library + + + + + + - - -
-

Library

-

Add books to your virtual library

-
+
+
+

Library

+

Add books to your virtual library

+
+ + + +
+ +
+
+ + +
+ +
+ + +
- +
+ + +
-
-
- - - - - - - - - /> +
+ + +
+ + +
-
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + + + + + + + + + + + + +
TitleAuthorNumber of PagesReadAction
+
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 1de7c0c1..a6694e3b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -7,7 +7,7 @@ window.addEventListener("load", function (e) { function populateStorage() { if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", @@ -24,9 +24,10 @@ 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() { +// Rename 'submit' to 'addBook' to avoid naming collisions +function addBook(e) { + if (e) e.preventDefault(); // Stops page from reloading on submit + if (!title.value || !author.value || !pages.value) { alert("Please fill all fields!"); return false; @@ -34,7 +35,7 @@ function submit() { let book = new Book(title.value, author.value, pages.value, check.checked); myLibrary.push(book); - // Add these lines to clear the text boxes after adding: + // Clear text boxes after adding title.value = ""; author.value = ""; pages.value = ""; @@ -53,50 +54,48 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - //delete old table + + // Clear old rows except header for (let n = rowsNumber - 1; n > 0; n--) { table.deleteRow(n); } - //insert updated row and cells + + // Insert updated rows in correct chronological order let length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = table.insertRow(-1); // Fix: Append to end instead of index 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 + titleCell.innerText = myLibrary[i].title; + authorCell.innerText = myLibrary[i].author; + pagesCell.innerText = myLibrary[i].pages; + + // Read status toggle button let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; + changeBut.className = myLibrary[i].check + ? "btn btn-success" + : "btn btn-secondary"; + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; + const index = i; // Store current index scope changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + myLibrary[index].check = !myLibrary[index].check; render(); }); - //add delete button to every row and render again + // Delete button let delBut = document.createElement("button"); - delBut.id = i + 5; deleteCell.appendChild(delBut); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; + delBut.innerText = "Delete"; delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); + alert(`You've deleted title: ${myLibrary[index].title}`); + myLibrary.splice(index, 1); render(); }); } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..de06e21b 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,19 @@ .form-group { - width: 400px; - height: 300px; - align-self: left; + max width: 400px; + height: 100px; + margin-bottom: 12px; padding-left: 20px; } .btn { display: block; } +button.btn-info { + margin: 20px; +} .form-check-label { padding-left: 20px; margin: 5px 0px 5px 0px; } -button.btn-info { - margin: 20px; -} From b79bf9f8f4595a2ec43bda76a650fc4bed1726ef Mon Sep 17 00:00:00 2001 From: TTiamiyu Date: Wed, 5 Aug 2026 05:56:42 +0100 Subject: [PATCH 5/5] Refactor form handling and improve book submission logic with input validation and status messages --- debugging/book-library/index.html | 39 +++--- debugging/book-library/script.js | 194 +++++++++++++++++------------- 2 files changed, 130 insertions(+), 103 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 31a191b8..e7d3828a 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -6,14 +6,15 @@ Virtual Library - - + + + @@ -23,34 +24,36 @@

Library

Add books to your virtual library

+ + + -
- -
-
- - +
+ +
+ +
-
- - +
+ +
-
- - +
+ +
-
+
- +
@@ -65,12 +68,10 @@

Library

- +
- - \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index a6694e3b..9bfa7ca0 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,102 +1,128 @@ -let myLibrary = []; +// Data store +const myLibrary = []; -window.addEventListener("load", function (e) { - populateStorage(); - render(); -}); - -function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robinson Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true +// DOM Element References with clear suffixes +const bookForm = document.getElementById("book-form"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); +const bookListBody = document.getElementById("book-list"); +const statusMessageEl = document.getElementById("status-message"); + +// Book Constructor +function Book(title, author, pages, check) { + this.title = title; + this.author = author; + this.pages = pages; // Stored as Number + this.check = check; // Stored as Boolean +} + +// Populate default books once on load +function initLibrary() { + if (myLibrary.length === 0) { + myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", 252, true)); + myLibrary.push( + 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"); - -// Rename 'submit' to 'addBook' to avoid naming collisions -function addBook(e) { - if (e) e.preventDefault(); // Stops page from reloading on submit - - if (!title.value || !author.value || !pages.value) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); - - // Clear text boxes after adding - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; - - render(); +// Preprocess and handle form submission +function handleFormSubmit(event) { + event.preventDefault(); + + // Input Sanitization & Normalization (.trim() and Number conversion) + const titleValue = titleInput.value.trim(); + const authorValue = authorInput.value.trim(); + const pagesValue = Number(pagesInput.value); + const isRead = checkInput.checked; + + // Validation check + if (!titleValue || !authorValue || isNaN(pagesValue) || pagesValue <= 0) { + showStatus("Please enter valid book details.", "danger"); + return; } -} -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; + + // Create and add new book + const newBook = new Book(titleValue, authorValue, pagesValue, isRead); + myLibrary.push(newBook); + + // Reset form inputs + bookForm.reset(); + + // Render updated table + render(); } +// Efficient DOM Rendering function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; + // Fast clearing of table rows without loops + bookListBody.textContent = ""; - // Clear old rows except header - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + myLibrary.forEach((book, index) => { + const row = document.createElement("tr"); + + const titleCell = document.createElement("td"); + titleCell.textContent = book.title; // Safe textContent preventing XSS - // Insert updated rows in correct chronological order - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(-1); // Fix: Append to end instead of index 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.innerText = myLibrary[i].title; - authorCell.innerText = myLibrary[i].author; - pagesCell.innerText = myLibrary[i].pages; - - // Read status toggle button - let changeBut = document.createElement("button"); - changeBut.className = myLibrary[i].check - ? "btn btn-success" - : "btn btn-secondary"; - changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; - wasReadCell.appendChild(changeBut); - - const index = i; // Store current index scope - changeBut.addEventListener("click", function () { - myLibrary[index].check = !myLibrary[index].check; + const authorCell = document.createElement("td"); + authorCell.textContent = book.author; + + const pagesCell = document.createElement("td"); + pagesCell.textContent = book.pages; + + // Toggle Read Button + const wasReadCell = document.createElement("td"); + const toggleReadBtn = document.createElement("button"); + toggleReadBtn.className = book.check + ? "btn btn-success btn-sm" + : "btn btn-secondary btn-sm"; + toggleReadBtn.textContent = book.check ? "Yes" : "No"; // Concise ternary operator + + toggleReadBtn.addEventListener("click", () => { + book.check = !book.check; render(); }); + wasReadCell.appendChild(toggleReadBtn); + + // Delete Button + const deleteCell = document.createElement("td"); + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-danger btn-sm"; + deleteBtn.textContent = "Delete"; - // Delete button - let delBut = document.createElement("button"); - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerText = "Delete"; - delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[index].title}`); + deleteBtn.addEventListener("click", () => { + const deletedTitle = book.title; + + // Perform deletion FIRST myLibrary.splice(index, 1); render(); + + // Display non-blocking feedback AFTER deletion + showStatus(`You deleted "${deletedTitle}" from your library.`, "info"); }); - } + deleteCell.appendChild(deleteBtn); + + // Append cells to row + row.append(titleCell, authorCell, pagesCell, wasReadCell, deleteCell); + bookListBody.appendChild(row); + }); } + +// Non-blocking status message UI helper +function showStatus(message, type = "info") { + statusMessageEl.textContent = message; + statusMessageEl.className = `alert alert-${type}`; + statusMessageEl.classList.remove("d-none"); + + // Auto-hide after 3 seconds + setTimeout(() => { + statusMessageEl.classList.add("d-none"); + }, 3000); +} + +// Event Listeners (Module compatible) +bookForm.addEventListener("submit", handleFormSubmit); +window.addEventListener("DOMContentLoaded", initLibrary);