From d2fb66a1e05fc0b19b7620f3063e0897ecd51652 Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 28 Nov 2025 21:21:41 +0000 Subject: [PATCH 1/5] Refactor book form and submission logic; enhance validation and local storage handling --- debugging/book-library/index.html | 90 ++++++++++++------------- debugging/book-library/script.js | 108 +++++++++++++++++------------- 2 files changed, 103 insertions(+), 95 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..80f84ac4 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -27,48 +27,49 @@

Library

Add new book -
-
- +
+
+
+ + + + + + +
+ type="checkbox" + class="form-check-input" + id="check" + value="" + />Read + +
+ +
@@ -81,16 +82,9 @@

Library

- - - - - - -
- + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..121c71ae 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,23 +1,27 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); }); +// Attach form submit event listener +document.getElementById("book-form").addEventListener("submit", function (event) { + event.preventDefault(); // Prevent default form submission + 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(); + const storedLibrary = localStorage.getItem("myLibrary"); + if (storedLibrary) { + myLibrary = JSON.parse(storedLibrary); + } else { + 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); + localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); } + render(); } const title = document.getElementById("title"); @@ -25,22 +29,42 @@ 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() { + console.log("submit() function called"); + console.log("Input values:", { + title: title.value, + author: author.value, + pages: pages.value, + read: check.checked + }); + + const maxPages = 30000; // Maximum allowed pages + const pageCount = parseInt(pages.value); if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + !title.value.trim() || + !author.value.trim() || + !pages.value || + isNaN(pageCount) || + pageCount <= 0 || + pageCount > maxPages ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + console.log("Validation failed: One or more fields are empty or invalid"); + alert( + `Please fill all fields with valid data! Pages must be a number between 1 and ${maxPages}.` + ); + return; } + + let book = new Book(title.value.trim(), author.value.trim(), pageCount, check.checked); + console.log("New book created:", book); + myLibrary.push(book); + console.log("myLibrary after push:", myLibrary); + localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); + render(); + document.querySelector("form").reset(); + console.log("Form reset"); + $("#demo").collapse("hide"); + console.log("Form collapsed"); } function Book(title, author, pages, check) { @@ -53,14 +77,11 @@ 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-- { + 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); + for (let i = 0; i < myLibrary.length; i++) { + let row = table.insertRow(-1); let titleCell = row.insertCell(0); let authorCell = row.insertCell(1); let pagesCell = row.insertCell(2); @@ -70,34 +91,27 @@ 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.id = `read-${i}`; changeBut.className = "btn btn-success"; + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; + localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); 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 = `delete-${i}`; + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + deleteCell.appendChild(delButton); + delButton.addEventListener("click", function () { alert(`You've deleted title: ${myLibrary[i].title}`); myLibrary.splice(i, 1); + localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); render(); }); } -} +} \ No newline at end of file From 96b6342b2f5de54dd9dc5570a5dadf0ff7a30e6f Mon Sep 17 00:00:00 2001 From: Ali Date: Wed, 3 Dec 2025 14:46:01 +0000 Subject: [PATCH 2/5] Refactor HTML structure and JavaScript logic; improve form handling and input validation --- debugging/book-library/index.html | 76 +++++++------- debugging/book-library/script.js | 158 +++++++++++++++--------------- 2 files changed, 119 insertions(+), 115 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 80f84ac4..2608cfb9 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,12 @@ - - - - + + My Book Library + + /> @@ -15,20 +15,18 @@ 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

+
- + -
-
+
Library value="" />Read +
- -
+
- - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + + + + + + + + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
- - + + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 121c71ae..ccada589 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,70 +1,45 @@ let myLibrary = []; -window.addEventListener("load", function () { +window.addEventListener("load", function (e) { populateStorage(); render(); }); -// Attach form submit event listener -document.getElementById("book-form").addEventListener("submit", function (event) { - event.preventDefault(); // Prevent default form submission - submit(); -}); - function populateStorage() { - const storedLibrary = localStorage.getItem("myLibrary"); - if (storedLibrary) { - myLibrary = JSON.parse(storedLibrary); - } else { + 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); - localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); } - render(); } -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"); +// Preprocess input before adding a book function submit() { - console.log("submit() function called"); - console.log("Input values:", { - title: title.value, - author: author.value, - pages: pages.value, - read: check.checked - }); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = parseInt(pagesInput.value.trim(), 10); + const isRead = readCheckbox.checked; - const maxPages = 30000; // Maximum allowed pages - const pageCount = parseInt(pages.value); - if ( - !title.value.trim() || - !author.value.trim() || - !pages.value || - isNaN(pageCount) || - pageCount <= 0 || - pageCount > maxPages - ) { - console.log("Validation failed: One or more fields are empty or invalid"); - alert( - `Please fill all fields with valid data! Pages must be a number between 1 and ${maxPages}.` - ); + if (!title || !author || isNaN(pages)) { + alert("Please fill all fields correctly!"); return; } - let book = new Book(title.value.trim(), author.value.trim(), pageCount, check.checked); - console.log("New book created:", book); + let book = new Book(title, author, pages, isRead); myLibrary.push(book); - console.log("myLibrary after push:", myLibrary); - localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); render(); - document.querySelector("form").reset(); - console.log("Form reset"); - $("#demo").collapse("hide"); - console.log("Form collapsed"); + + // Clear input fields after submission + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readCheckbox.checked = false; } function Book(title, author, pages, check) { @@ -74,44 +49,67 @@ 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; - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); + const table = document.getElementById("display"); + let tbody = table.querySelector("tbody"); + + // If doesn't exist, create it + if (!tbody) { + tbody = document.createElement("tbody"); + table.appendChild(tbody); } - for (let i = 0; i < myLibrary.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; - - let changeBut = document.createElement("button"); - changeBut.id = `read-${i}`; - changeBut.className = "btn btn-success"; - changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; - wasReadCell.appendChild(changeBut); - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; - localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); + + // Clear existing rows in + tbody.innerHTML = ""; + + myLibrary.forEach((book, index) => { + 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 + document.querySelectorAll(".toggle-read").forEach(button => { + button.addEventListener("click", (e) => { + const index = e.target.dataset.index; + myLibrary[index].check = !myLibrary[index].check; render(); }); + }); - let delButton = document.createElement("button"); - delButton.id = `delete-${i}`; - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; - deleteCell.appendChild(delButton); - delButton.addEventListener("click", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); + document.querySelectorAll(".delete-book").forEach(button => { + button.addEventListener("click", (e) => { + const index = e.target.dataset.index; + myLibrary.splice(index, 1); render(); + + // 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); }); - } -} \ No newline at end of file + }); +} + +// Make the submit function globally accessible +window.submit = submit; \ No newline at end of file From 61da010b4f91bb15ac3947bc752f0a90cad19b13 Mon Sep 17 00:00:00 2001 From: Ali Date: Wed, 3 Dec 2025 18:22:48 +0000 Subject: [PATCH 3/5] Update debugging/book-library/index.html Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- debugging/book-library/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 2608cfb9..631358b7 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -91,6 +91,6 @@

Library

- + \ No newline at end of file From 25b0dd38ca57809c357f7384d3fdc4b31e60ed8c Mon Sep 17 00:00:00 2001 From: Ali Date: Wed, 3 Dec 2025 18:23:22 +0000 Subject: [PATCH 4/5] Update debugging/book-library/script.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- debugging/book-library/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index ccada589..6c8c0cc0 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -76,7 +76,7 @@ function render() { pagesCell.textContent = book.pages; const readCell = row.insertCell(); - readCell.innerHTML = ``; + readCell.innerHTML = ``; const actionsCell = row.insertCell(); actionsCell.innerHTML = ``; From e66aa7db72ecb634275163195be337e88f315c1c Mon Sep 17 00:00:00 2001 From: Ali Date: Thu, 4 Dec 2025 00:19:29 +0000 Subject: [PATCH 5/5] Refactor HTML structure and JavaScript logic; improve form handling, input validation, and styling --- debugging/book-library/index.html | 156 +++++++++++++++--------------- debugging/book-library/script.js | 147 ++++++++++++++-------------- debugging/book-library/style.css | 4 +- 3 files changed, 150 insertions(+), 157 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 631358b7..52396d53 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,10 @@ - - My Book Library - + + My Book Library + + + @@ -15,82 +13,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

+
- + -
-
- - - - - - -
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + + + + + + + + + + + + + + + + + + +
TitleAuthorNumber of PagesRead
- - + + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 6c8c0cc0..ff7e2c6a 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,115 +1,112 @@ let myLibrary = []; +// DOM refs +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); +const submitBtn = document.getElementById("submitBtn"); +const table = document.getElementById("display"); + 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); + 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); } } -// 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"); +// wire submit button +submitBtn.addEventListener("click", addBook); -// Preprocess input before adding a book -function submit() { - const title = titleInput.value.trim(); - const author = authorInput.value.trim(); - const pages = parseInt(pagesInput.value.trim(), 10); - const isRead = readCheckbox.checked; +// validate and add book to library +function addBook() { + // trim to avoid spaces-only input + const t = titleInput.value.trim(); + const a = authorInput.value.trim(); + const pRaw = pagesInput.value.trim(); - if (!title || !author || isNaN(pages)) { - alert("Please fill all fields correctly!"); + if (!t || !a || !pRaw) { + alert("Please fill all fields!"); return; } - let book = new Book(title, author, pages, isRead); + // pages should be a positive integer (prevent "weird" page counts) + const pNum = Number(pRaw); + if (!Number.isFinite(pNum) || pNum <= 0 || !Number.isInteger(pNum)) { + alert("Please enter a valid positive whole number of pages."); + return; + } + + const book = new Book(t, a, pNum, checkInput.checked); myLibrary.push(book); - render(); - // Clear input fields after submission + // clear inputs and close the collapse titleInput.value = ""; authorInput.value = ""; pagesInput.value = ""; - readCheckbox.checked = false; + checkInput.checked = false; + + render(); } -function Book(title, author, pages, check) { +// Book constructor +function Book(title, author, pages, read) { this.title = title; this.author = author; this.pages = pages; - this.check = check; + this.read = !!read; } -// Efficiently clear table rows +// render table rows from myLibrary function render() { - const table = document.getElementById("display"); - let tbody = table.querySelector("tbody"); - - // If doesn't exist, create it - if (!tbody) { - tbody = document.createElement("tbody"); - table.appendChild(tbody); - } - - // Clear existing rows in + const tbody = table.querySelector("tbody"); tbody.innerHTML = ""; - myLibrary.forEach((book, index) => { + for (let i = 0; i < myLibrary.length; i++) { + const book = myLibrary[i]; 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 titleCell = row.insertCell(0); + const authorCell = row.insertCell(1); + const pagesCell = row.insertCell(2); + const wasReadCell = row.insertCell(3); + const deleteCell = row.insertCell(4); - const pagesCell = row.insertCell(); + titleCell.textContent = book.title; + authorCell.textContent = book.author; pagesCell.textContent = book.pages; - const readCell = row.insertCell(); - readCell.innerHTML = ``; - - const actionsCell = row.insertCell(); - actionsCell.innerHTML = ``; - }); - - // 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; + // Read toggle button + const changeBut = document.createElement("button"); + changeBut.className = "btn btn-sm btn-outline-success"; + changeBut.textContent = book.read ? "Yes" : "No"; + changeBut.addEventListener("click", function() { + myLibrary[i].read = !myLibrary[i].read; render(); }); - }); - - document.querySelectorAll(".delete-book").forEach(button => { - button.addEventListener("click", (e) => { - const index = e.target.dataset.index; - myLibrary.splice(index, 1); - render(); - - // 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); + wasReadCell.appendChild(changeBut); + + // Delete button + const delBut = document.createElement("button"); + delBut.className = "btn btn-sm btn-warning"; + delBut.textContent = "Delete"; + delBut.addEventListener("click", function() { + if (confirm(`Delete "${myLibrary[i].title}"?`)) { + myLibrary.splice(i, 1); + render(); + } }); - }); -} - -// Make the submit function globally accessible -window.submit = submit; \ No newline at end of file + deleteCell.appendChild(delBut); + } +} \ No newline at end of file diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..aa17204c 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,7 +1,5 @@ .form-group { width: 400px; - height: 300px; - align-self: left; padding-left: 20px; } @@ -16,4 +14,4 @@ button.btn-info { margin: 20px; -} +} \ No newline at end of file