From 3300f4bdd83fbe68883ee8a52417ce6234ea9ccd Mon Sep 17 00:00:00 2001 From: loveth900 Date: Tue, 25 Mar 2025 16:12:07 +0000 Subject: [PATCH 1/2] fixed the debugging --- debugging/book-library/script.js | 139 ++++++++++++++++--------------- 1 file changed, 71 insertions(+), 68 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1..017db58 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,103 +1,106 @@ let myLibrary = []; -window.addEventListener("load", function (e) { - populateStorage(); - render(); +// Event listener for window load +window.addEventListener("load", function () { + populateStorage(); // Populate the library with initial data if empty + render(); // Render the library to display books }); +// The Book constructor to create book objects +function Book(title, author, pages, check) { + this.title = title; + this.author = author; + this.pages = pages; + this.check = check; +} + +// Populate the library with books, either from localStorage or default values function populateStorage() { - if (myLibrary.length == 0) { + // If there are no books in localStorage, populate with default books + if (localStorage.getItem("myLibrary") === null) { let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); + let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true); myLibrary.push(book1); myLibrary.push(book2); - render(); - } -} - -const title = document.getElementById("title"); -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 || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; + saveToLocalStorage(); // Save initial data to localStorage } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + myLibrary = JSON.parse(localStorage.getItem("myLibrary")); // Load books from localStorage } } -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; -} - +// Function to render the books to the HTML table function render() { let table = document.getElementById("display"); + // Clear the current table rows (except the header row) 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); + + // Insert updated rows for each book in myLibrary + myLibrary.forEach((book, i) => { + let row = table.insertRow(); 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 + titleCell.innerHTML = book.title; + authorCell.innerHTML = book.author; + pagesCell.innerHTML = book.pages; + + // Create a button to toggle the read/unread status of the book 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"; - } + let readStatus = book.check ? "Yes" : "No"; changeBut.innerText = readStatus; + wasReadCell.appendChild(changeBut); + // Event listener to toggle the book's read status changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; - render(); + book.check = !book.check; + saveToLocalStorage(); // Save changes to localStorage + render(); // Re-render the table }); - //add delete button to every row and render again + // Create a delete button for each book let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); + delButton.className = "btn btn-warning"; + delButton.innerHTML = "Delete"; + deleteCell.appendChild(delButton); + + // Event listener to delete the book + delButton.addEventListener("click", function () { + alert(`You've deleted title: ${book.title}`); myLibrary.splice(i, 1); - render(); + saveToLocalStorage(); // Save updated library to localStorage + render(); // Re-render the table }); + }); +} + +// Function to save myLibrary to localStorage +function saveToLocalStorage() { + localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); +} + +// Form element references +const title = document.getElementById("title"); +const author = document.getElementById("author"); +const pages = document.getElementById("pages"); +const check = document.getElementById("check"); + +// Submit function to add a new book +function submit() { + // Validate form fields + if (title.value === "" || pages.value === "") { + alert("Please fill in all fields!"); + return false; + } else { + let book = new Book(title.value, author.value, pages.value, check.checked); + myLibrary.push(book); // Add the new book to the library + saveToLocalStorage(); // Save updated library to localStorage + render(); // Re-render the table } } From 0e7494a59a74cb960b02ec8fb2aa48f258440132 Mon Sep 17 00:00:00 2001 From: loveth900 Date: Wed, 9 Apr 2025 08:29:08 +0100 Subject: [PATCH 2/2] the error implementation have been fixed --- debugging/book-library/index.html | 5 ++ debugging/book-library/script.js | 132 ++++++++++++++++-------------- 2 files changed, 74 insertions(+), 63 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa7..1fb254f 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -69,6 +69,11 @@

Library

/> + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 017db58..065c3b3 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,12 +1,10 @@ let myLibrary = []; -// Event listener for window load window.addEventListener("load", function () { - populateStorage(); // Populate the library with initial data if empty - render(); // Render the library to display books + populateStorage(); + render(); }); -// The Book constructor to create book objects function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -14,93 +12,101 @@ function Book(title, author, pages, check) { this.check = check; } -// Populate the library with books, either from localStorage or default values function populateStorage() { - // If there are no books in localStorage, populate with default books if (localStorage.getItem("myLibrary") === null) { - 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); - saveToLocalStorage(); // Save initial data to localStorage + 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); + saveToLocalStorage(); } else { - myLibrary = JSON.parse(localStorage.getItem("myLibrary")); // Load books from localStorage + myLibrary = JSON.parse(localStorage.getItem("myLibrary")); } } -// Function to render the books to the HTML table -function render() { - let table = document.getElementById("display"); - // Clear the current table rows (except the header row) - let rowsNumber = table.rows.length; - for (let n = rowsNumber - 1; n > 0; n--) { - table.deleteRow(n); +function render(sortBy = null) { + const table = document.getElementById("display"); + + // Remove all rows except the header efficiently + while (table.rows.length > 1) { + table.deleteRow(1); + } + + if (sortBy === "title") { + myLibrary.sort((a, b) => a.title.localeCompare(b.title)); + } else if (sortBy === "pages") { + myLibrary.sort((a, b) => a.pages - b.pages); } - // Insert updated rows for each book in myLibrary - myLibrary.forEach((book, i) => { - let row = table.insertRow(); - 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 = book.title; - authorCell.innerHTML = book.author; - pagesCell.innerHTML = book.pages; - - // Create a button to toggle the read/unread status of the book - let changeBut = document.createElement("button"); - changeBut.className = "btn btn-success"; - let readStatus = book.check ? "Yes" : "No"; - changeBut.innerText = readStatus; - wasReadCell.appendChild(changeBut); - - // Event listener to toggle the book's read status - changeBut.addEventListener("click", function () { + myLibrary.forEach((book, index) => { + const row = table.insertRow(); + 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); + + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + const toggleReadButton = document.createElement("button"); + toggleReadButton.className = "btn btn-success"; + toggleReadButton.textContent = book.check ? "Yes" : "No"; + wasReadCell.appendChild(toggleReadButton); + + toggleReadButton.addEventListener("click", function () { book.check = !book.check; - saveToLocalStorage(); // Save changes to localStorage - render(); // Re-render the table + saveToLocalStorage(); + render(); }); - // Create a delete button for each book - let delButton = document.createElement("button"); - delButton.className = "btn btn-warning"; - delButton.innerHTML = "Delete"; - deleteCell.appendChild(delButton); + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + deleteCell.appendChild(deleteButton); - // Event listener to delete the book - delButton.addEventListener("click", function () { + deleteButton.addEventListener("click", function () { + myLibrary.splice(index, 1); + saveToLocalStorage(); + render(); alert(`You've deleted title: ${book.title}`); - myLibrary.splice(i, 1); - saveToLocalStorage(); // Save updated library to localStorage - render(); // Re-render the table }); }); } -// Function to save myLibrary to localStorage function saveToLocalStorage() { localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); } -// Form element references +// Form elements const title = document.getElementById("title"); const author = document.getElementById("author"); const pages = document.getElementById("pages"); const check = document.getElementById("check"); -// Submit function to add a new book function submit() { - // Validate form fields - if (title.value === "" || pages.value === "") { + const titleValue = title.value.trim(); + const authorValue = author.value.trim(); + const pagesValue = pages.value.trim(); + + if (!titleValue || !authorValue || !pagesValue) { alert("Please fill in all fields!"); return false; - } else { - let book = new Book(title.value, author.value, pages.value, check.checked); - myLibrary.push(book); // Add the new book to the library - saveToLocalStorage(); // Save updated library to localStorage - render(); // Re-render the table } + + const pagesNumber = Number(pagesValue); + + if (!Number.isInteger(pagesNumber) || pagesNumber <= 0) { + alert("Pages must be a positive whole number!"); + return false; + } + + const newBook = new Book(titleValue, authorValue, pagesNumber, check.checked); + myLibrary.push(newBook); + saveToLocalStorage(); + render(); } + +// Optional: Add sorting buttons +document.getElementById("sortTitle").addEventListener("click", () => render("title")); +document.getElementById("sortPages").addEventListener("click", () => render("pages"));