diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index 23acfa71..d5b1e6cc 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -1,75 +1,76 @@
-
+
-
-
-
-
-
+ Book Library
+
+
+
-
+
-
+
-
+
+
+
+
@@ -77,20 +78,20 @@ Library
Author |
Number of Pages |
Read |
- |
+ Actions |
+
-
- |
- |
- |
- |
- |
-
+
+
+
+
+
+
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1d..0748840f 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -1,48 +1,69 @@
let myLibrary = [];
-window.addEventListener("load", function (e) {
+// =====> Load initial data
+window.addEventListener("load", function () {
populateStorage();
render();
});
+// =====> Default books
function populateStorage() {
- if (myLibrary.length == 0) {
- let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
+ 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",
+ 127,
true
);
- myLibrary.push(book1);
- myLibrary.push(book2);
- render();
+
+ myLibrary.push(book1, book2);
}
}
+// =====> DOM elements
const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
+const bookForm = document.getElementById("book-form");
+
+// =====> Form submit
+bookForm.addEventListener("submit", function (e) {
+ e.preventDefault();
+ submit();
+});
-//check the right input from forms and if its ok -> add the new book (object in array)
-//via Book function and start render function
+// =====> Add new book
function submit() {
- if (
- title.value == null ||
- title.value == "" ||
- pages.value == null ||
- pages.value == ""
- ) {
+ const titleValue = title.value.trim();
+ const authorValue = author.value.trim();
+ const pagesValue = pages.value.trim();
+
+ // Validation (improved)
+ if (!titleValue || !authorValue || !pagesValue) {
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;
}
+
+ // Create new book
+ let book = new Book(
+ titleValue,
+ authorValue,
+ Number(pagesValue),
+ check.checked
+ );
+
+ myLibrary.push(book);
+
+ render();
+ bookForm.reset();
+
+ // safely close bootstrap form (optional chaining operator)
+ document.getElementById("demo")?.classList.remove("show");
}
+// =====> Book constructor
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
@@ -50,54 +71,52 @@ function Book(title, author, pages, check) {
this.check = check;
}
+// =====> 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);
+
+ // clear old rows (keep header)
+ for (let i = table.rows.length - 1; i > 0; i--) {
+ table.deleteRow(i);
}
- //insert updated row and cells
- let length = myLibrary.length;
- for (let i = 0; i < length; i++) {
+
+ // render books
+ myLibrary.forEach((book, index) => {
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;
+ let readCell = row.insertCell(3);
+ let actionCell = row.insertCell(4);
+
+ titleCell.textContent = book.title;
+ authorCell.textContent = book.author;
+ pagesCell.textContent = book.pages;
+
+ // =====> Read button
+ let readBtn = document.createElement("button");
+ readBtn.className = "btn btn-success";
+ readBtn.textContent = book.check ? "Read" : "Unread";
+
+ readBtn.addEventListener("click", function () {
+ book.check = !book.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);
+ readCell.appendChild(readBtn);
+
+ // =====> Delete button
+ let deleteBtn = document.createElement("button");
+ deleteBtn.className = "btn btn-warning";
+ deleteBtn.textContent = "Delete";
+
+ deleteBtn.addEventListener("click", function () {
+ alert(`Deleted: ${book.title}`);
+ myLibrary.splice(index, 1);
render();
});
- }
+
+ actionCell.appendChild(deleteBtn);
+ });
}
diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css
index 302950cb..698589ca 100644
--- a/debugging/book-library/style.css
+++ b/debugging/book-library/style.css
@@ -1,19 +1,70 @@
+html {
+ scroll-behavior: smooth;
+}
+
+.header {
+ background: #2f2e57;
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+ padding: 20px;
+}
+
+.header h1 {
+ color: white;
+ font-size: 50px;
+ margin: 10px 0;
+}
+
+.header p {
+ color: white;
+ font-size: 30px;
+ margin: 10px 0;
+}
+
.form-group {
+ background-color: #2f2e57;
+ color: whitesmoke;
width: 400px;
- height: 300px;
- align-self: left;
- padding-left: 20px;
+ height: auto;
+ margin: 20px auto;
+ padding: 20px;
+ border-radius: 10px;
+
+ display: flex;
+ flex-direction: column;
+ gap: 10px;
}
.btn {
- display: block;
+ background-color: #4b4a7a;
+ color: white;
+ margin: 20px auto;
+ padding: 10px 20px;
+ border-radius: 10px;
+ font-size: 18px;
+ border: none;
+ cursor: pointer;
+ transition: 0.2s ease;
}
-.form-check-label {
- padding-left: 20px;
- margin: 5px 0px 5px 0px;
+.btn:focus {
+ outline: none;
+ box-shadow: none;
+}
+
+.btn-read {
+ background-color: #4b4a7a;
+ color: white;
}
-button.btn-info {
- margin: 20px;
+.btn-delete {
+ background-color: #a94442;
+ color: white;
+}
+
+.form-check-label {
+ padding-left: 20px;
+ margin: 5px 0;
}