diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index 23acfa71..c1d714ba 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -1,12 +1,9 @@
-
-
+
+
+
@@ -31,19 +28,17 @@ Library
Library
|
-
|
|
@@ -88,7 +82,6 @@ Library
|
|
-
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1d..150efd35 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -1,3 +1,5 @@
+
+
let myLibrary = [];
window.addEventListener("load", function (e) {
@@ -7,16 +9,15 @@ window.addEventListener("load", function (e) {
function populateStorage() {
if (myLibrary.length == 0) {
- let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
+ let book1 = new Book("Robison 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();
}
}
@@ -28,19 +29,27 @@ 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!");
+ const cleanTitle = title.value.trim();
+ const cleanAuthor = author.value.trim();
+
+ if (!/^[1-9]\d*$/.test(pages.value.trim())) { // plain positive whole numbers like 1, 25, 300 — no letters, no decimal points, no scientific notation
+ alert("Pages must be a positive whole number without letters or symbols.");
return false;
- } else {
- let book = new Book(title.value, title.value, pages.value, check.checked);
- library.push(book);
- render();
}
+
+ const cleanPages = parseInt(pages.value.trim(), 10);
+
+ if (!cleanTitle|| !cleanAuthor) { // title and author doesn't allowed to contain only space characters
+ alert("Title and Author cannot be empty or spaces only.");
+ return false;
+
+ } if (isNaN(cleanPages) || cleanPages <= 0) { // the value of type of pages must be a integer
+ alert("Pages must be a positive whole number.");
+ return false;
+ }
+ let book = new Book(cleanTitle, cleanAuthor, pages.value, check.checked); //// title and author be allowed to contain leading or trailing space characters, we storage using parseInt
+ myLibrary.push(book);
+ render();
}
function Book(title, author, pages, check) {
@@ -52,11 +61,10 @@ function Book(title, author, pages, check) {
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);
- }
+ // Keep only the header row
+ table.innerHTML = table.rows[0].outerHTML;
+
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
@@ -66,37 +74,35 @@ function render() {
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 = myLibrary[i].title;
+ authorCell.textContent = myLibrary[i].author;
+ pagesCell.textContent = 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 toggleReadButton = document.createElement("button");
+ toggleReadButton.className = "btn btn-success";
+ wasReadCell.appendChild(toggleReadButton);
let readStatus = "";
- if (myLibrary[i].check == false) {
- readStatus = "Yes";
- } else {
- readStatus = "No";
- }
- changeBut.innerText = readStatus;
-
- changeBut.addEventListener("click", function () {
+
+ readStatus = myLibrary[i].check ? "Yes" : "No";
+
+ toggleReadButton.innerText = readStatus;
+
+ toggleReadButton.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);
+ let deleteBookButton = document.createElement("button");
+ deleteCell.appendChild(deleteBookButton);
+ deleteBookButton.className = "btn btn-warning";
+ deleteBookButton.innerHTML = "Delete";
+ deleteBookButton.addEventListener("click", function () {
+ const deleteTitle = myLibrary[i].title; //store first
+ myLibrary.splice(i, 1); // delete it
+ alert(`You have deleted title: ${deleteTitle}`);
render();
});
}