+
+
+
+
+
+ | Title |
+ Author |
+ Pages |
+ Read |
+ |
+
+
+
+
+
+
+
+
+
+
-
-
-
- | Title |
- Author |
- Number of Pages |
- Read |
- |
-
-
-
-
- |
- |
- |
- |
- |
-
-
-
+
+
+
-
-
-
+
\ No newline at end of file
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1d..44ecc4e7 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -1,103 +1,123 @@
+// Local storage key
+const STORAGE_KEY = "myLibraryBooks";
+
+// Book list
let myLibrary = [];
-window.addEventListener("load", function (e) {
- populateStorage();
+// Cached DOM elements
+const title = document.getElementById("title");
+const author = document.getElementById("author");
+const pages = document.getElementById("pages");
+const check = document.getElementById("check");
+const addBtn = document.getElementById("addBtn");
+const displayBody = document.querySelector("#display tbody");
+
+function Book(title, author, pages, read) {
+ this.title = title;
+ this.author = author;
+ this.pages = pages;
+ this.read = !!read;
+}
+
+// Load from localStorage on page start
+window.addEventListener("DOMContentLoaded", () => {
+ loadLibrary();
render();
});
-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();
+// Load saved books or create default sample books
+function loadLibrary() {
+ const stored = localStorage.getItem(STORAGE_KEY);
+
+ if (stored) {
+ myLibrary = JSON.parse(stored);
+ } else {
+ // First-time sample books
+ myLibrary = [
+ new Book("Robinson Crusoe", "Daniel Defoe", 252, true),
+ new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true)
+ ];
+ saveLibrary();
}
}
-const title = document.getElementById("title");
-const author = document.getElementById("author");
-const pages = document.getElementById("pages");
-const check = document.getElementById("check");
+// Save books to localStorage
+function saveLibrary() {
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(myLibrary));
+}
-//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 == ""
- ) {
+// Clear form after adding
+function clearForm() {
+ title.value = "";
+ author.value = "";
+ pages.value = "";
+ check.checked = false;
+}
+
+// Submit new book
+function submitBook() {
+ if (!title.value.trim() || !author.value.trim() || !pages.value) {
alert("Please fill all fields!");
- return false;
- } else {
- let book = new Book(title.value, title.value, pages.value, check.checked);
- library.push(book);
- render();
+ return;
}
-}
-function Book(title, author, pages, check) {
- this.title = title;
- this.author = author;
- this.pages = pages;
- this.check = check;
+ const book = new Book(
+ title.value.trim(),
+ author.value.trim(),
+ Number(pages.value),
+ check.checked
+ );
+
+ myLibrary.push(book);
+ saveLibrary();
+ render();
+ clearForm();
+
+ // Collapse form
+ $("#addForm").collapse("hide");
}
+addBtn.addEventListener("click", submitBook);
+
+// Render table
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;
-
- //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 == false) {
- readStatus = "Yes";
- } else {
- readStatus = "No";
- }
- changeBut.innerText = readStatus;
-
- changeBut.addEventListener("click", function () {
- myLibrary[i].check = !myLibrary[i].check;
+ displayBody.innerHTML = "";
+
+ myLibrary.forEach((b, i) => {
+ const row = document.createElement("tr");
+
+ row.innerHTML = `
+
${b.title} |
+
${b.author} |
+
${b.pages} |
+
|
+
|
+ `;
+
+ // Read button
+ const readBtn = document.createElement("button");
+ readBtn.className = "btn btn-sm btn-success btn-small";
+ readBtn.textContent = b.read ? "Yes" : "No";
+ readBtn.addEventListener("click", () => {
+ myLibrary[i].read = !myLibrary[i].read;
+ saveLibrary();
render();
});
+ row.children[3].appendChild(readBtn);
- //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 () {
- alert(`You've deleted title: ${myLibrary[i].title}`);
- myLibrary.splice(i, 1);
- render();
+ // Delete button
+ const delBtn = document.createElement("button");
+ delBtn.className = "btn btn-sm btn-warning btn-small";
+ delBtn.textContent = "Delete";
+ delBtn.addEventListener("click", () => {
+ if (confirm(`Delete "${b.title}"?`)) {
+ myLibrary.splice(i, 1);
+ saveLibrary();
+ render();
+ }
});
- }
+ row.children[4].appendChild(delBtn);
+
+ displayBody.appendChild(row);
+ });
}
From 2087dd43ec5d1781738656e32bc18b916b9eac8f Mon Sep 17 00:00:00 2001
From: shaghayeghfar <146011477+shaghayeghfar@users.noreply.github.com>
Date: Sat, 6 Dec 2025 15:44:52 +0000
Subject: [PATCH 2/3] changes based of feedback
---
debugging/book-library/index.html | 25 +++---
debugging/book-library/script.js | 122 +++++++++++++++++-------------
2 files changed, 82 insertions(+), 65 deletions(-)
diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index 3e860253..d8ac14b6 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -6,7 +6,9 @@
My Book Library
+
+