-
+
\ No newline at end of file
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1d..f4076785 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -1,103 +1,180 @@
+// Book class (must be defined before use)
+class Book {
+ constructor(title, author, pages, check) {
+ this.title = title;
+ this.author = author;
+ this.pages = pages;
+ this.check = check;
+ }
+}
+
+// initialise an empty array to hold my book library
let myLibrary = [];
-window.addEventListener("load", function (e) {
- populateStorage();
- render();
-});
+// Dom elements
+const title = document.getElementById("title");
+const author = document.getElementById("author");
+const pages = document.getElementById("pages");
+const check = document.getElementById("check");
+const progressHeader = document.getElementById("progress-header");
+const progressBar = document.getElementById("progress-bar");
+const table = document.getElementById("display");
+
+// function to save my library to localStorage
+function saveToStorage() {
+ // convert myLibrary array to a JSON string and save it
+ localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
+}
+
+// function to load my library from localStorage
+function loadFromStorage() {
+ // retrieve the JSON string from localStorage and parse it back to an array
+ const stored = localStorage.getItem("myLibrary");
+ // only parse if there is something stored
+ if (stored) {
+ myLibrary = JSON.parse(stored);
+ }
+}
+
+// function to display the current number of books read vs unread
+function updateProgressBar() {
+ const totalBooks = myLibrary.length;
+ const readBooks = myLibrary.filter((book) => book.check).length;
+ const percent =
+ totalBooks === 0 ? 0 : Math.round((readBooks / totalBooks) * 100);
+
+ // update the progress bar text
+ progressHeader.innerText = `Read ${readBooks} out of ${totalBooks} books`;
+
+ // update the progress bar width
+ progressBar.style.width = percent + "%";
+}
function populateStorage() {
+ // add default books to myLibrary if it's empty on initial setup
if (myLibrary.length == 0) {
- let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
+ 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);
- myLibrary.push(book2);
+ myLibrary.push(book1, book2);
render();
}
}
-const title = document.getElementById("title");
-const author = document.getElementById("author");
-const pages = document.getElementById("pages");
-const check = document.getElementById("check");
+// clear the form after submission
+function clearForm() {
+ title.value = "";
+ author.value = "";
+ pages.value = "";
+ check.checked = false;
+}
-//check the right input from forms and if its ok -> add the new book (object in array)
-//via Book function and start render function
+// handle form submission - check the right input from forms and if its ok -> add the new book (object in array)
function submit() {
- if (
- title.value == null ||
- title.value == "" ||
- pages.value == null ||
- pages.value == ""
- ) {
+ // add validation for empty fields
+ if (title.value == "" || author.value == "" || 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);
+ // create a new book object and add it to myLibrary and save to storage
+ let book = new Book(title.value, author.value, pages.value, check.checked);
+ myLibrary.push(book);
+ saveToStorage(); // save the book after adding
render();
+ clearForm();
}
}
-function Book(title, author, pages, check) {
- this.title = title;
- this.author = author;
- this.pages = pages;
- this.check = check;
-}
-
+// render the table with books from myLibrary
function render() {
- let table = document.getElementById("display");
+ // count the existing number of rows in the table
let rowsNumber = table.rows.length;
- //delete old table
- for (let n = rowsNumber - 1; n > 0; n-- {
+ //delete all the rows except for the table header row (delete all rows except row 0)
+ 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;
- 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 () {
- alert(`You've deleted title: ${myLibrary[i].title}`);
- myLibrary.splice(i, 1);
- render();
- });
+ // update the progress bar after deleting rows
+ updateProgressBar();
+
+ // add rows for each book in myLibrary
+ myLibrary.forEach((book, i) => {
+ createBookRow(book, i);
+ });
+}
+
+// create a table row for each book
+function createBookRow(book, i) {
+ let row = table.insertRow(1); // insert new row at position 1 to keep the header intact
+ 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);
+
+ // fill the first 3 cells with book data
+ 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 changeBtn = document.createElement("button");
+ changeBtn.id = i;
+ wasReadCell.appendChild(changeBtn);
+ setReadButtonState(changeBtn, book.check);
+ changeBtn.addEventListener("click", () => handleToggleRead(i));
+
+ //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", () => {
+ handleDeleteBook(i);
+ });
+}
+
+function setReadButtonState(btn, isRead) {
+ // set the button text based on the read status
+ if (isRead) {
+ btn.innerHTML = "Read";
+ btn.className = "btn btn-success";
+ } else {
+ btn.innerHTML = "Unread";
+ btn.className = "btn btn-danger";
}
}
+
+// toggle the read status of a book
+function handleToggleRead(index) {
+ // toggle the read status of the book at the given index
+ myLibrary[index].check = !myLibrary[index].check;
+ // save to storage and re-render
+ saveToStorage();
+ render();
+}
+
+// delete a book from the library
+function handleDeleteBook(index) {
+ alert("You have deleted title: " + myLibrary[index].title);
+ // remove the book at the given index from myLibrary
+ myLibrary.splice(index, 1);
+ saveToStorage();
+ render();
+}
+
+// when the window loads, we want to load the saved books from local storage
+window.addEventListener("load", function (e) {
+ // load saved books from local storage
+ loadFromStorage();
+ // only add default books if myLibrary is empty
+ // this prevents overwriting existing books
+ if (myLibrary.length === 0) {
+ populateStorage();
+ }
+ render();
+});
diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css
index 302950cb..232ad125 100644
--- a/debugging/book-library/style.css
+++ b/debugging/book-library/style.css
@@ -1,19 +1,32 @@
-.form-group {
- width: 400px;
- height: 300px;
- align-self: left;
- padding-left: 20px;
+body {
+ background-color: #f8f9fa;
+ font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
-.btn {
- display: block;
+.table th {
+ background-color: #343a40;
+ color: white;
}
-.form-check-label {
- padding-left: 20px;
- margin: 5px 0px 5px 0px;
+/* Responsive table wrapper */
+.table-responsive {
+ width: 100%;
+ overflow-x: auto;
+}
+/* Optional: Make sure body/container doesn't restrict table width */
+body,
+html {
+ width: 100%;
+ margin: 0;
+ padding: 0;
+ box-sizing: border-box;
+}
+
+.btn {
+ margin: 2px;
+ border-radius: 20px;
}
-button.btn-info {
- margin: 20px;
+.card {
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}