diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index 23acfa7..eaaa313 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -1,12 +1,8 @@
-
+
-
-
+
+ Sabita Book Library
@@ -14,7 +10,7 @@
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
-
+
@@ -36,7 +32,7 @@ Library
id="title"
name="title"
required
- />
+ >
Library
id="author"
name="author"
required
- />
+ >
Library
id="pages"
name="pages"
required
- />
+ >
+ >
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1..7050547 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -28,20 +28,25 @@ 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 == ""
- ) {
+ const titleValue =title.value.trim();
+ const authorValue = author.value.trim();
+ const pagesValue = pages.value.trim();
+ 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);
+ }
+
+ const pagesInt = parseInt(pagesValue, 10);
+ if (isNaN(pagesInt) || pagesInt <= 0) {
+ alert("Please enter a valid number of pages!");
+ return false;
+
+
+ }
+ let book = new Book(title.value, author.value, pagesInt, check.checked);
+ myLibrary.push(book);
render();
}
-}
function Book(title, author, pages, check) {
this.title = title;
@@ -52,52 +57,50 @@ function Book(title, author, pages, check) {
function render() {
let table = document.getElementById("display");
- let rowsNumber = table.rows.length;
+ let tbody = table.querySelector("tbody");
+
+
//delete old table
- for (let n = rowsNumber - 1; n > 0; n-- {
- table.deleteRow(n);
- }
+ tbody.innerHTML = "";
+""
+
//insert updated row and cells
- let length = myLibrary.length;
- for (let i = 0; i < length; i++) {
- let row = table.insertRow(1);
+ myLibrary.forEach((book, index) => {
+ let row = tbody.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;
+ titleCell.textContent = book.title;
+ authorCell.textContent = book.author;
+ pagesCell.textContent = book.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 changeReadStatusButton = createButton("btn btn-success", book.check ? "yes" : "No");
+ wasReadCell.appendChild(changeReadStatusButton);
+ changeReadStatusButton.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);
- render();
+ let deleteButton = createButton("btn btn-warning", "Delete");
+ deleteCell.appendChild(deleteButton);
+ deleteButton.addEventListener("click", function () {
+ alert(`You've deleted title: ${book.title}`);
+ myLibrary.splice(index, 1); // Remove the book from the array
+ render();
});
- }
+ });
}
+function createButton(className, text) {
+ let button = document.createElement("button");
+ button.className = className;
+ button.textContent = text;
+ return button;
+}
+
+