From 82ea23c54b440da735106c9c29cb86feb7a1b3c9 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Sat, 29 Nov 2025 14:58:50 +0000 Subject: [PATCH 1/3] Debug Book Library --- debugging/book-library/script.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..44c1a552 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(); } } @@ -37,8 +36,8 @@ 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); render(); } } @@ -54,7 +53,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 +75,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"; @@ -90,11 +89,11 @@ function render() { //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 () { + 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 619df7f08423a123107d648821d99ac48427c411 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Mon, 1 Dec 2025 19:54:19 +0000 Subject: [PATCH 2/3] update index.html and script.js as per feedback --- debugging/book-library/index.html | 154 +++++++++++++++--------------- debugging/book-library/script.js | 126 +++++++++++------------- 2 files changed, 133 insertions(+), 147 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..72bc4891 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,12 @@ - - - - + + My Book Library + + /> @@ -15,82 +15,82 @@ href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" /> - - - -
-

Library

-

Add books to your virtual library

-
+ + +
+

Library

+

Add books to your virtual library

+
- + -
-
- - - - - - - +
+
+ + + + + + +
+ type="checkbox" + class="form-check-input" + id="check" + value="" + />Read + +
+
+
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + + + + + + + + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
- - + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 44c1a552..40249f67 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -6,40 +6,40 @@ window.addEventListener("load", function (e) { }); 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) { + 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); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +// Use meaningful variable names for DOM elements +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = 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 +// Preprocess input before adding a book 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, author.value, pages.value, check.checked); - myLibrary.push(book); - render(); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = parseInt(pagesInput.value.trim(), 10); + const isRead = readCheckbox.checked; + + if (!title || !author || isNaN(pages)) { + alert("Please fill all fields correctly!"); + return; } + + let book = new Book(title, author, pages, isRead); + myLibrary.push(book); + render(); + + // Clear input fields after submission + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readCheckbox.checked = false; } function Book(title, author, pages, check) { @@ -49,54 +49,40 @@ function Book(title, author, pages, check) { this.check = check; } +// Efficiently clear table rows 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); - } - //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; + const table = document.getElementById("display"); + table.innerHTML = "TitleAuthorPagesReadActions"; - //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 == true) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; + myLibrary.forEach((book, index) => { + const row = table.insertRow(); + row.innerHTML = ` + ${book.title} + ${book.author} + ${book.pages} + + + `; + }); - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + // Add event listeners for buttons + document.querySelectorAll(".toggle-read").forEach(button => { + button.addEventListener("click", (e) => { + const index = e.target.dataset.index; + myLibrary[index].check = !myLibrary[index].check; render(); }); + }); - //add delete button to every row and render again - 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}`); - myLibrary.splice(i, 1); + document.querySelectorAll(".delete-book").forEach(button => { + button.addEventListener("click", (e) => { + const index = e.target.dataset.index; + myLibrary.splice(index, 1); render(); + alert("Book deleted successfully."); }); - } + }); } + +// Make the submit function globally accessible +window.submit = submit; From d29419e7e64333515ec4a3d1b8b5e7ecd982e811 Mon Sep 17 00:00:00 2001 From: enjoy15 Date: Tue, 2 Dec 2025 04:12:29 +0000 Subject: [PATCH 3/3] script.js amended to address extra comments. --- debugging/book-library/script.js | 47 +++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 40249f67..98205703 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -52,17 +52,34 @@ function Book(title, author, pages, check) { // Efficiently clear table rows function render() { const table = document.getElementById("display"); - table.innerHTML = "TitleAuthorPagesReadActions"; + let tbody = table.querySelector("tbody"); + + // If doesn't exist, create it + if (!tbody) { + tbody = document.createElement("tbody"); + table.appendChild(tbody); + } + + // Clear existing rows in + tbody.innerHTML = ""; myLibrary.forEach((book, index) => { - const row = table.insertRow(); - row.innerHTML = ` - ${book.title} - ${book.author} - ${book.pages} - - - `; + const row = tbody.insertRow(); + + const titleCell = row.insertCell(); + titleCell.textContent = book.title; // Escapes special characters + + const authorCell = row.insertCell(); + authorCell.textContent = book.author; // Escapes special characters + + const pagesCell = row.insertCell(); + pagesCell.textContent = book.pages; + + const readCell = row.insertCell(); + readCell.innerHTML = ``; + + const actionsCell = row.insertCell(); + actionsCell.innerHTML = ``; }); // Add event listeners for buttons @@ -79,7 +96,17 @@ function render() { const index = e.target.dataset.index; myLibrary.splice(index, 1); render(); - alert("Book deleted successfully."); + + // Display a non-blocking notification + const notification = document.createElement("div"); + notification.textContent = "Book deleted successfully."; + notification.className = "notification"; + document.body.appendChild(notification); + + // Remove the notification after 3 seconds + setTimeout(() => { + notification.remove(); + }, 3000); }); }); }