From 5f7b018bdbe5774816426b64bba2073d0bdf57ea Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Thu, 16 Apr 2026 15:26:54 +0100 Subject: [PATCH 1/3] Fix: syntax errors --- debugging/book-library/script.js | 86 +++++++++++++++++++------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..27a074e3 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -25,22 +25,44 @@ 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() { + // Validate all fields (including author) if ( title.value == null || - title.value == "" || + title.value.trim() == "" || + author.value == null || + author.value.trim() == "" || pages.value == null || - pages.value == "" + pages.value.trim() == "" ) { - alert("Please fill all fields!"); + alert("Please fill all fields (Title, Author, Pages)!"); + return false; + } + + // Validate pages is a positive number + const pagesNum = parseInt(pages.value, 10); + if (isNaN(pagesNum) || pagesNum <= 0) { + alert("Please enter a valid number of pages!"); return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); } + + // FIX: use author.value instead of title.value for the second argument + let book = new Book( + title.value.trim(), + author.value.trim(), + pagesNum, + check.checked + ); + myLibrary.push(book); // FIX: was 'library.push' + render(); + + // Clear form fields for better UX + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; + + return true; } function Book(title, author, pages, check) { @@ -52,52 +74,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-- { - table.deleteRow(n); + + // Delete all rows except the header (row 0) + // FIX: missing ')' in original for-loop – replaced with while loop for clarity + while (table.rows.length > 1) { + table.deleteRow(1); } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); + + // Re-populate table with current library + for (let i = 0; i < myLibrary.length; i++) { + let row = table.insertRow(); // appends at the end (preserves order) + 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 status toggle 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; - + // FIX: correct "Yes" when read, "No" when not read + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); + wasReadCell.appendChild(changeBut); - //add delete button to every row and render again + // Delete button 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.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + // FIX: event type 'click' (was 'clicks') and correct variable name + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); render(); }); + deleteCell.appendChild(delButton); } } From 7a4aca0700678ca3e08e6d0653440ecdded722e4 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Fri, 17 Apr 2026 14:52:23 +0100 Subject: [PATCH 2/3] Minor HTML fixes and corresponding changes in script.js --- debugging/book-library/index.html | 73 ++++----------- debugging/book-library/script.js | 149 +++++++++++++----------------- 2 files changed, 83 insertions(+), 139 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..e1e6ddcd 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,19 +1,12 @@ - - + My Book Library + - + @@ -30,43 +23,22 @@

Library

- - - + + + + + + - + + + - + + +
@@ -80,17 +52,10 @@

Library

- - - - - - - - - + - + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 27a074e3..f47b26da 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,121 +1,100 @@ +// DOM references +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = document.getElementById("check"); +const submitBtn = document.getElementById("submitBtn"); + let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", () => { populateStorage(); render(); + submitBtn.addEventListener("click", submit); }); 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(); + if (myLibrary.length === 0) { + const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); + myLibrary.push(book1, book2); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); - -function submit() { - // Validate all fields (including author) - if ( - title.value == null || - title.value.trim() == "" || - author.value == null || - author.value.trim() == "" || - pages.value == null || - pages.value.trim() == "" - ) { +function submit(event) { + event.preventDefault(); // prevent any form-like behavior + + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pagesRaw = pagesInput.value.trim(); + const isRead = readCheckbox.checked; + + if (!title || !author || !pagesRaw) { alert("Please fill all fields (Title, Author, Pages)!"); - return false; + return; } - // Validate pages is a positive number - const pagesNum = parseInt(pages.value, 10); - if (isNaN(pagesNum) || pagesNum <= 0) { - alert("Please enter a valid number of pages!"); - return false; + const pagesNum = Number(pagesRaw); + if (!Number.isInteger(pagesNum) || pagesNum <= 0) { + alert("Please enter a valid positive number of pages!"); + return; } - // FIX: use author.value instead of title.value for the second argument - let book = new Book( - title.value.trim(), - author.value.trim(), - pagesNum, - check.checked - ); - myLibrary.push(book); // FIX: was 'library.push' + const newBook = new Book(title, author, pagesNum, isRead); + myLibrary.push(newBook); render(); - // Clear form fields for better UX - title.value = ""; - author.value = ""; - pages.value = ""; - check.checked = false; - - return true; + // Clear form + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readCheckbox.checked = false; } -function Book(title, author, pages, check) { +function Book(title, author, pages, isRead) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.check = isRead; } function render() { - let table = document.getElementById("display"); - - // Delete all rows except the header (row 0) - // FIX: missing ')' in original for-loop – replaced with while loop for clarity - while (table.rows.length > 1) { - table.deleteRow(1); - } + const tableBody = document.querySelector("#display tbody"); + tableBody.innerHTML = ""; - // Re-populate table with current library for (let i = 0; i < myLibrary.length; i++) { - let row = table.insertRow(); // appends at the end (preserves order) - - 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; - - // Read status toggle button - let changeBut = document.createElement("button"); - changeBut.className = "btn btn-success"; - // FIX: correct "Yes" when read, "No" when not read - changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + const book = myLibrary[i]; + const row = tableBody.insertRow(); + + const titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const readCell = row.insertCell(3); + const deleteCell = row.insertCell(4); + + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + // Toggle read button + const toggleBtn = document.createElement("button"); + toggleBtn.className = "btn btn-success"; + toggleBtn.textContent = book.check ? "Yes" : "No"; + toggleBtn.addEventListener("click", () => { + book.check = !book.check; render(); }); - wasReadCell.appendChild(changeBut); + readCell.appendChild(toggleBtn); // Delete button - let delButton = document.createElement("button"); - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; - // FIX: event type 'click' (was 'clicks') and correct variable name - delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-warning"; + deleteBtn.textContent = "Delete"; + deleteBtn.addEventListener("click", () => { myLibrary.splice(i, 1); + alert(`You've deleted title: ${book.title}`); render(); }); - deleteCell.appendChild(delButton); + deleteCell.appendChild(deleteBtn); } } From ef264a8370b2ecc731c2b38a904edd9b8f627b49 Mon Sep 17 00:00:00 2001 From: Carlos Abreu Date: Fri, 17 Apr 2026 15:19:12 +0100 Subject: [PATCH 3/3] Minor HTML fix to conform to validator.w3.org service. --- debugging/book-library/index.html | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index e1e6ddcd..1f5cb7f3 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -2,7 +2,8 @@ My Book Library - + + @@ -26,18 +27,15 @@

Library

- - - @@ -55,7 +53,6 @@

Library

-