From fa8157cd31f85d77975bb9060131b4d0fa83d859 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Wed, 12 Aug 2026 12:30:13 +0100 Subject: [PATCH 1/5] Fix bugs in book library --- debugging/book-library/script.js | 34 +++++++++++++------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..2b501ff53 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -25,8 +25,6 @@ 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 || @@ -37,8 +35,10 @@ 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); + // Bug 3 fixed: was using title.value twice instead of author.value + let book = new Book(title.value, author.value, pages.value, check.checked); + // Bug 2 fixed: was using undefined `library` instead of `myLibrary` + myLibrary.push(book); render(); } } @@ -53,11 +53,10 @@ function Book(title, author, pages, check) { function render() { let table = document.getElementById("display"); let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { + // Bug 1 fixed: missing closing parenthesis on for loop + 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++) { let row = table.insertRow(1); @@ -70,17 +69,12 @@ function render() { 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"; - } + // Bug 5 fixed: inverted read status (true means read, show "Yes"; false means not read, show "No") + let readStatus = myLibrary[i].check ? "Yes" : "No"; changeBut.innerText = readStatus; changeBut.addEventListener("click", function () { @@ -88,13 +82,13 @@ function render() { render(); }); - //add delete button to every row and render again + // Bug 4 fixed: variable name was `delBut` but declared as `delButton`; also event was "clicks" not "click" let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + delButton.id = i + 5; + deleteCell.appendChild(delButton); + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); From f2f464a51fc64ef2ccc09af0f56e5e5dcd088ea0 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Thu, 13 Aug 2026 08:54:54 +0100 Subject: [PATCH 2/5] fix: resolve W3C html syntax validation errors and clean up script logic --- debugging/book-library/index.html | 116 +++++++++++++-------------- debugging/book-library/script.js | 125 ++++++++++++++++-------------- 2 files changed, 124 insertions(+), 117 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..2eb19a211 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - + - - + + + Library App @@ -15,6 +12,8 @@ href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" /> + + @@ -28,46 +27,54 @@

Library

-
- - - - - - -
+
@@ -77,20 +84,13 @@

Library

- + - - - - - - - - + + +
Author Number of Pages ReadActions
- - - + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 2b501ff53..6d879079d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,46 +1,47 @@ -let myLibrary = []; +const myLibrary = []; -window.addEventListener("load", function (e) { +// Refactor: Removed duplicate render() execution on load. populateStorage() handles initial render. +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); + if (myLibrary.length === 0) { + // Refactor: Stored "page count" data type explicitly as numbers instead of strings + 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"); +// Refactor: Updated variable names with descriptive suffixes to clearly reflect that they store DOM nodes +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); function submit() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); + // Refactor: Preprocessing input through trim validation and numerical parsing (no need to check for null on values) + const formattedTitle = titleInput.value.trim(); + const formattedAuthor = authorInput.value.trim(); + const pageCount = parseInt(pagesInput.value, 10); + + // Input validation constraint check + if (!formattedTitle || !formattedAuthor || isNaN(pageCount) || pageCount <= 0) { + alert("Please fill all fields with valid information!"); return false; - } else { - // Bug 3 fixed: was using title.value twice instead of author.value - let book = new Book(title.value, author.value, pages.value, check.checked); - // Bug 2 fixed: was using undefined `library` instead of `myLibrary` - myLibrary.push(book); - render(); } + + let book = new Book(formattedTitle, formattedAuthor, pageCount, checkInput.checked); + myLibrary.push(book); + render(); + + // Clear inputs on success + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; } function Book(title, author, pages, check) { @@ -51,47 +52,53 @@ function Book(title, author, pages, check) { } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - // Bug 1 fixed: missing closing parenthesis on for loop - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } - let length = myLibrary.length; + // Refactor: Targets the new clean semantic table body ID from your index.html + const tableBody = document.getElementById("book-table-body"); + + // Refactor: Efficient batch approach to delete table rows at once by clearing container instead of looping one-by-one + tableBody.innerHTML = ""; + + const length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + let row = tableBody.insertRow(-1); // Inserts cleanly sequentially at the end 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; - - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - // Bug 5 fixed: inverted read status (true means read, show "Yes"; false means not read, show "No") - let readStatus = myLibrary[i].check ? "Yes" : "No"; - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { + + // Refactor: Replaced unsafe innerHTML with secure textContent to prevent cross-site scripting + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = myLibrary[i].pages; + + // Refactor: Naming consistency (statusBtn & deleteBtn) and dropped redundant tracking IDs + const statusBtn = document.createElement("button"); + statusBtn.className = "btn btn-success btn-sm"; + wasReadCell.appendChild(statusBtn); + + // Refactor: Simplified verbose if-else block to one statement using the ternary ? : operator + statusBtn.textContent = myLibrary[i].check ? "Yes" : "No"; + + statusBtn.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); - // Bug 4 fixed: variable name was `delBut` but declared as `delButton`; also event was "clicks" not "click" - let delButton = document.createElement("button"); - delButton.id = i + 5; - deleteCell.appendChild(delButton); - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; - delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deleteBtn = document.createElement("button"); + deleteCell.appendChild(deleteBtn); + deleteBtn.className = "btn btn-warning btn-sm"; + deleteBtn.textContent = "Delete"; + + deleteBtn.addEventListener("click", function () { + const targetTitle = myLibrary[i].title; + // Refactor: Resolved timing bug. Data mutation and UI render occur safely BEFORE the blocking window.alert runs myLibrary.splice(i, 1); render(); + alert(`You've successfully deleted title: ${targetTitle}`); }); } } + +// Bind submission trigger securely to the window instance for internal module routing +window.submit = submit; From 9979cbcc2a95b062eaa8a312897a61ee811533d8 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Wed, 19 Aug 2026 10:25:29 +0100 Subject: [PATCH 3/5] fix: refactor event handling to addEventListener and use native form reset --- debugging/book-library/index.html | 4 +-- debugging/book-library/script.js | 42 +++++++++++-------------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 2eb19a211..2fe9e0d2d 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -28,7 +28,7 @@

Library

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

Library

- \ No newline at end of file + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 6d879079d..5d1284970 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,13 +1,17 @@ +// 1. Declare all shared variables at the beginning of the file const myLibrary = []; +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"); -// Refactor: Removed duplicate render() execution on load. populateStorage() handles initial render. window.addEventListener("load", function () { populateStorage(); }); function populateStorage() { if (myLibrary.length === 0) { - // Refactor: Stored "page count" data type explicitly as numbers instead of strings 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); @@ -15,34 +19,26 @@ function populateStorage() { } } -// Refactor: Updated variable names with descriptive suffixes to clearly reflect that they store DOM nodes -const titleInput = document.getElementById("title"); -const authorInput = document.getElementById("author"); -const pagesInput = document.getElementById("pages"); -const checkInput = document.getElementById("check"); +// 2. Attach event listener in JS via .addEventListener() instead of HTML inline +bookForm.addEventListener("submit", function (event) { + event.preventDefault(); // Stop native page reload -function submit() { - // Refactor: Preprocessing input through trim validation and numerical parsing (no need to check for null on values) const formattedTitle = titleInput.value.trim(); const formattedAuthor = authorInput.value.trim(); const pageCount = parseInt(pagesInput.value, 10); - // Input validation constraint check if (!formattedTitle || !formattedAuthor || isNaN(pageCount) || pageCount <= 0) { alert("Please fill all fields with valid information!"); - return false; + return; } let book = new Book(formattedTitle, formattedAuthor, pageCount, checkInput.checked); myLibrary.push(book); render(); - // Clear inputs on success - titleInput.value = ""; - authorInput.value = ""; - pagesInput.value = ""; - checkInput.checked = false; -} + // 3. Use the form's native method to reset all fields cleanly + bookForm.reset(); +}); function Book(title, author, pages, check) { this.title = title; @@ -52,32 +48,25 @@ function Book(title, author, pages, check) { } function render() { - // Refactor: Targets the new clean semantic table body ID from your index.html const tableBody = document.getElementById("book-table-body"); - - // Refactor: Efficient batch approach to delete table rows at once by clearing container instead of looping one-by-one tableBody.innerHTML = ""; const length = myLibrary.length; for (let i = 0; i < length; i++) { - let row = tableBody.insertRow(-1); // Inserts cleanly sequentially at the end + let row = tableBody.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); - // Refactor: Replaced unsafe innerHTML with secure textContent to prevent cross-site scripting titleCell.textContent = myLibrary[i].title; authorCell.textContent = myLibrary[i].author; pagesCell.textContent = myLibrary[i].pages; - // Refactor: Naming consistency (statusBtn & deleteBtn) and dropped redundant tracking IDs const statusBtn = document.createElement("button"); statusBtn.className = "btn btn-success btn-sm"; wasReadCell.appendChild(statusBtn); - - // Refactor: Simplified verbose if-else block to one statement using the ternary ? : operator statusBtn.textContent = myLibrary[i].check ? "Yes" : "No"; statusBtn.addEventListener("click", function () { @@ -92,7 +81,6 @@ function render() { deleteBtn.addEventListener("click", function () { const targetTitle = myLibrary[i].title; - // Refactor: Resolved timing bug. Data mutation and UI render occur safely BEFORE the blocking window.alert runs myLibrary.splice(i, 1); render(); alert(`You've successfully deleted title: ${targetTitle}`); @@ -100,5 +88,3 @@ function render() { } } -// Bind submission trigger securely to the window instance for internal module routing -window.submit = submit; From 2959de5dece6d8d20e0ec25ff040ef3f66e0e434 Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Wed, 19 Aug 2026 10:39:59 +0100 Subject: [PATCH 4/5] refactor: optimize deletion alert with setTimeout to fix UI blocking --- debugging/book-library/script.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 5d1284970..55cff5a06 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -81,9 +81,18 @@ function render() { deleteBtn.addEventListener("click", function () { const targetTitle = myLibrary[i].title; + + // 1. Remove item from memory array myLibrary.splice(i, 1); + + // 2. Force DOM row structure updates render(); - alert(`You've successfully deleted title: ${targetTitle}`); + + // 3. Advanced Fix: Defer alert thread-blocking to the next event loop tick. + // This forces the browser to physically clear the table row BEFORE freezing. + setTimeout(() => { + alert(`You've successfully deleted title: ${targetTitle}`); + }, 0); }); } } From 17f407d9012618a0ef9f51ca527d5f4858eb122a Mon Sep 17 00:00:00 2001 From: KhotKeys Date: Wed, 19 Aug 2026 18:46:26 +0100 Subject: [PATCH 5/5] refactor: isolate startup code into init() and convert submit to named function --- debugging/book-library/script.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 55cff5a06..ab56bea14 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,9 +6,15 @@ const authorInput = document.getElementById("author"); const pagesInput = document.getElementById("pages"); const checkInput = document.getElementById("check"); -window.addEventListener("load", function () { +// 2. Clear app initialization entry point +window.addEventListener("load", init); + +function init() { populateStorage(); -}); + + // Grouping event listener registration inside initialization function + bookForm.addEventListener("submit", handleFormSubmit); +} function populateStorage() { if (myLibrary.length === 0) { @@ -19,8 +25,8 @@ function populateStorage() { } } -// 2. Attach event listener in JS via .addEventListener() instead of HTML inline -bookForm.addEventListener("submit", function (event) { +// 3. Extracted "on submit" logic into a clean named callback function +function handleFormSubmit(event) { event.preventDefault(); // Stop native page reload const formattedTitle = titleInput.value.trim(); @@ -36,9 +42,9 @@ bookForm.addEventListener("submit", function (event) { myLibrary.push(book); render(); - // 3. Use the form's native method to reset all fields cleanly + // Use the form's native method to reset all fields cleanly bookForm.reset(); -}); +} function Book(title, author, pages, check) { this.title = title; @@ -81,15 +87,9 @@ function render() { deleteBtn.addEventListener("click", function () { const targetTitle = myLibrary[i].title; - - // 1. Remove item from memory array myLibrary.splice(i, 1); - - // 2. Force DOM row structure updates render(); - // 3. Advanced Fix: Defer alert thread-blocking to the next event loop tick. - // This forces the browser to physically clear the table row BEFORE freezing. setTimeout(() => { alert(`You've successfully deleted title: ${targetTitle}`); }, 0);