From 8cf25d12a7d5e43f0bc5cbbb997388a84fb4e454 Mon Sep 17 00:00:00 2001 From: Fayaz Date: Tue, 14 Apr 2026 15:54:13 +0100 Subject: [PATCH 1/4] - Fixed syntax error in render loop (missing closing parenthesis). - Corrected variable reference from 'library' to 'myLibrary'. - Fixed logic bug where title input was being used for the author field. - Fixed delete button functionality and event listener naming. - Corrected read status display logic to show "Yes" when checked. --- debugging/book-library/script.js | 71 +++++++++++++++++--------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..48a9c4ad 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", @@ -16,29 +16,37 @@ function populateStorage() { ); myLibrary.push(book1); myLibrary.push(book2); - render(); + // render() is called in the window load listener, so no need to call it here twice } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = 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() { + // Bug fix: Check all fields including author if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + titleInput.value === "" || + authorInput.value === "" || + pagesInput.value === "" ) { 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 Fix: Changed second argument from titleInput.value to authorInput.value + let book = new Book(titleInput.value, authorInput.value, pagesInput.value, checkInput.checked); + + // Bug 2 Fix: Changed 'library.push' to 'myLibrary.push' + myLibrary.push(book); + + // Clear inputs after submit + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + render(); } } @@ -53,51 +61,48 @@ 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 Fix: Added missing closing parenthesis in the for-loop 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++) { - let row = table.insertRow(1); + let row = table.insertRow(-1); // Changed to -1 to append 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; - //add and wait for action for read/unread button + // Read/Unread Button Logic 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; + + // Bug 5 Fix: Reversed the logic. If check is true, status is "Yes" + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); - //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); + // Bug 4 Fix: Corrected variable names (delButton vs delBut) and event listener name ("click") + let delBut = document.createElement("button"); delBut.className = "btn btn-warning"; delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { + deleteCell.appendChild(delBut); + + delBut.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); } -} +} \ No newline at end of file From c163676727036ae3a8ec973700e7e11d06b140c2 Mon Sep 17 00:00:00 2001 From: Fayaz Date: Tue, 14 Apr 2026 15:56:36 +0100 Subject: [PATCH 2/4] update input types for better browser compatibility - Changed input types for title and author to 'text' for standard validation. - Ensured script and bootstrap links are correctly ordered for DOM loading. --- debugging/book-library/index.html | 122 +++++++++++------------------- 1 file changed, 45 insertions(+), 77 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..7ba0149c 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,64 @@ - + - - + My Library + + - +

Library

-

Add books to your virtual library

+

Add books to your library

- +
+ -
-
- - - - - - - - +
+
+
+ + + + + + + + + +
+ + +
+ + +
+
-
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + + + + + + + + + + + +
TitleAuthorPagesRead StatusActions
+
- + \ No newline at end of file From 087eadc1137957c59752294e71e7b35d74350a71 Mon Sep 17 00:00:00 2001 From: Fayaz Date: Tue, 14 Apr 2026 15:58:29 +0100 Subject: [PATCH 3/4] refine layout for book form and action buttons - Adjusted form-group dimensions for better visibility. - Added spacing and alignment for table action buttons. --- debugging/book-library/style.css | 45 +++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..754b5e0c 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,44 @@ +body { + background-color: #f8f9fa; +} + +.jumbotron { + background-color: #343a40; + color: white; + border-radius: 0; +} + +.container { + margin-top: 20px; +} + + .form-group { - width: 400px; - height: 300px; - align-self: left; - padding-left: 20px; + max-width: 500px; + margin: 0 auto; } + .btn { - display: block; + margin-top: 5px; } -.form-check-label { - padding-left: 20px; - margin: 5px 0px 5px 0px; + +#display { + background-color: white; + box-shadow: 0 4px 8px rgba(0,0,0,0.05); + margin-top: 20px; } -button.btn-info { - margin: 20px; + +.form-check-label { + padding-left: 5px; + cursor: pointer; } + + +.btn-success, .btn-warning { + display: inline-block; + margin-right: 5px; + font-size: 0.85rem; +} \ No newline at end of file From 9df271a38f1b1c3904451aaf8845dbeaf4d5adb1 Mon Sep 17 00:00:00 2001 From: Fayaz Date: Fri, 17 Apr 2026 23:03:51 +0100 Subject: [PATCH 4/4] clean up redundant logic and implement code best practices. --- debugging/book-library/script.js | 121 +++++++++++++------------------ 1 file changed, 51 insertions(+), 70 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 48a9c4ad..2533e9c1 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,17 +6,11 @@ window.addEventListener("load", function (e) { }); 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 - ); - myLibrary.push(book1); - myLibrary.push(book2); - // render() is called in the window load listener, so no need to call it here twice + 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); + myLibrary.push(book1, book2); + } } @@ -26,83 +20,70 @@ const pagesInput = document.getElementById("pages"); const checkInput = document.getElementById("check"); function submit() { - // Bug fix: Check all fields including author - if ( - titleInput.value === "" || - authorInput.value === "" || - pagesInput.value === "" - ) { + + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = pagesInput.value; + + if (title === "" || author === "" || pages === "") { alert("Please fill all fields!"); - return false; - } else { - // Bug 3 Fix: Changed second argument from titleInput.value to authorInput.value - let book = new Book(titleInput.value, authorInput.value, pagesInput.value, checkInput.checked); - - // Bug 2 Fix: Changed 'library.push' to 'myLibrary.push' - myLibrary.push(book); - - // Clear inputs after submit - titleInput.value = ""; - authorInput.value = ""; - pagesInput.value = ""; - checkInput.checked = false; - - render(); + return; } + + const book = new Book(title, author, pages, checkInput.checked); + myLibrary.push(book); + + + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + checkInput.checked = false; + + render(); } function Book(title, author, pages, check) { this.title = title; this.author = author; - this.pages = pages; + this.pages = Number(pages); this.check = check; } function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - - // Bug Fix: Added missing closing parenthesis in the for-loop header - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); - } + const tableBody = document.querySelector("#display tbody"); + - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(-1); // Changed to -1 to append 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); + tableBody.innerHTML = ""; - titleCell.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - // Read/Unread Button Logic - let changeBut = document.createElement("button"); - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); + myLibrary.forEach((book, i) => { + const row = tableBody.insertRow(); - // Bug 5 Fix: Reversed the logic. If check is true, status is "Yes" - changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; + + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + + const wasReadCell = row.insertCell(3); + const changeBut = document.createElement("button"); + changeBut.className = "btn btn-success"; + changeBut.textContent = book.check ? "Yes" : "No"; + changeBut.onclick = () => { + book.check = !book.check; render(); - }); + }; + wasReadCell.appendChild(changeBut); - // Bug 4 Fix: Corrected variable names (delButton vs delBut) and event listener name ("click") - let delBut = document.createElement("button"); + + const deleteCell = row.insertCell(4); + const delBut = document.createElement("button"); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - deleteCell.appendChild(delBut); - - delBut.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + delBut.textContent = "Delete"; + delBut.onclick = () => { myLibrary.splice(i, 1); render(); - }); - } + console.log(`Deleted: ${book.title}`); + }; + deleteCell.appendChild(delBut); + }); } \ No newline at end of file