diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1d..5b55cac6 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -31,14 +31,16 @@ function submit() {
if (
title.value == null ||
title.value == "" ||
+ author.value == null ||
+ author.value == "" ||
pages.value == null ||
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);
+ let book = new Book(title.value, author.value, pages.value, check.checked);
+ myLibrary.push(book);
render();
}
}
@@ -54,7 +56,7 @@ function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//delete old table
- for (let n = rowsNumber - 1; n > 0; n-- {
+ for (let n = rowsNumber - 1; n > 0; n--) {
table.deleteRow(n);
}
//insert updated row and cells
@@ -76,7 +78,7 @@ function render() {
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
- if (myLibrary[i].check == false) {
+ if (myLibrary[i].check === true) {
readStatus = "Yes";
} else {
readStatus = "No";
@@ -89,12 +91,12 @@ function render() {
});
//add delete button to every row and render again
- let delButton = document.createElement("button");
+ let delBut = document.createElement("button");
delBut.id = i + 5;
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
- delBut.addEventListener("clicks", function () {
+ delBut.addEventListener("click", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
From bf2388c89aad1092782ac8d696d25c8d782f71c9 Mon Sep 17 00:00:00 2001
From: Emilianouz <135679131+Emilianouz@users.noreply.github.com>
Date: Thu, 7 Aug 2025 20:11:13 +0100
Subject: [PATCH 2/3] Issues solved according to the feedback. - Using user
input with validation. - Arguments are pre-processed/sanitize before calling
the constructor; removing leading/trailing whitespace - rows are inserted
inside the , not the
---
debugging/book-library/index.html | 10 +++---
debugging/book-library/script.js | 56 ++++++++++++++-----------------
2 files changed, 31 insertions(+), 35 deletions(-)
diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index 7c5dffdb..cdd0caef 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -1,7 +1,7 @@
-
+ Book Library
Library
-
Title
-
Author
-
Number of Pages
-
Read
+
Title
+
Author
+
Number of Pages
+
Read
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 5b55cac6..4a271fdc 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -2,16 +2,16 @@ let myLibrary = [];
window.addEventListener("load", function (e) {
populateStorage();
- render();
+ //render();
});
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);
@@ -20,26 +20,25 @@ function populateStorage() {
}
}
-const title = document.getElementById("title");
-const author = document.getElementById("author");
-const pages = document.getElementById("pages");
-const check = document.getElementById("check");
+const titleEl = document.getElementById("title");
+const authorEl = document.getElementById("author");
+const pagesEl = document.getElementById("pages");
+const checkEl = 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() {
+ const pageNumber = Number(pages.value);
if (
- title.value == null ||
- title.value == "" ||
- author.value == null ||
- author.value == "" ||
- pages.value == null ||
- pages.value == ""
+ titleEl.value.trim() === "" ||
+ authorEl.value.trim() === "" ||
+ isNaN(pageNumber) ||
+ pageNumber <= 0
) {
- alert("Please fill all fields!");
+ alert("Please fill all fields correctly!");
return false;
} else {
- let book = new Book(title.value, author.value, pages.value, check.checked);
+ let book = new Book(titleEl.value.toLowerCase().replace(/\b\w/g, s => s.toUpperCase()), authorEl.value.toLowerCase().replace(/\b\w/g, s => s.toUpperCase()), pageNumber, checkEl.checked);
myLibrary.push(book);
render();
}
@@ -54,15 +53,17 @@ 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);
+ let tbody = table.getElementsByTagName("tbody")[0];
+
+ // Set the innerHTML of the tbody to an empty string to clear all rows
+ if (tbody) {
+ tbody.innerHTML = "";
}
+
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
- let row = table.insertRow(1);
+ let row = tbody.insertRow();
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
@@ -77,14 +78,9 @@ function render() {
changeBut.id = i;
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
- let readStatus = "";
- if (myLibrary[i].check === true) {
- readStatus = "Yes";
- } else {
- readStatus = "No";
- }
- changeBut.innerText = readStatus;
+ changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
+
changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
@@ -92,13 +88,13 @@ function render() {
//add delete button to every row and render again
let delBut = document.createElement("button");
- delBut.id = i + 5;
+ //delBut.id = i + 5;
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
- delBut.innerHTML = "Delete";
+ delBut.innerText = "Delete";
delBut.addEventListener("click", function () {
- alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
+ alert(`You've deleted title: ${myLibrary[i].title}`);
render();
});
}
From 06a8a089df5c8c3792099f36e165bd56dff108c9 Mon Sep 17 00:00:00 2001
From: Emilianouz <135679131+Emilianouz@users.noreply.github.com>
Date: Fri, 8 Aug 2025 11:12:47 +0100
Subject: [PATCH 3/3] Errors in html fixed: - Attribute type updated in input
for author and title - Charset and name in separated tags. - Empty rows on
tbody removed. - html tag with language attribute. On script: Variable to
store name of book to show the name after it is deleted.
---
debugging/book-library/index.html | 20 +++++---------------
debugging/book-library/script.js | 4 ++--
2 files changed, 7 insertions(+), 17 deletions(-)
diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index cdd0caef..90d834ce 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -1,12 +1,9 @@
-
+
Book Library
-
+
+
@@ -31,7 +28,7 @@
Library
Library
/>
Library
-
-
-
-
-
-
-
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 4a271fdc..6f4c2543 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -88,13 +88,13 @@ function render() {
//add delete button to every row and render again
let delBut = document.createElement("button");
- //delBut.id = i + 5;
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerText = "Delete";
delBut.addEventListener("click", function () {
+ const nameBookDeleted = myLibrary[i].title;
myLibrary.splice(i, 1);
- alert(`You've deleted title: ${myLibrary[i].title}`);
+ alert(`You've deleted title: ${nameBookDeleted}`);
render();
});
}