Skip to content

Commit af5b973

Browse files
committed
Update the JavaScript according to the "feedback to improve the code"
1 parent 6992fad commit af5b973

File tree

1 file changed

+70
-41
lines changed

1 file changed

+70
-41
lines changed

debugging/book-library/script.js

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,97 +7,126 @@ window.addEventListener("load", function (e) {
77

88
function populateStorage() {
99
if (myLibrary.length == 0) {
10-
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
10+
let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true);
1111
let book2 = new Book(
1212
"The Old Man and the Sea",
1313
"Ernest Hemingway",
14-
"127",
14+
127,
1515
true
1616
);
1717
myLibrary.push(book1);
1818
myLibrary.push(book2);
19-
render();
2019
}
2120
}
2221

23-
const title = document.getElementById("title");
24-
const author = document.getElementById("author");
25-
const pages = document.getElementById("pages");
26-
const check = document.getElementById("check");
22+
const titleElement = document.getElementById("title");
23+
const authorElement = document.getElementById("author");
24+
const pagesElement = document.getElementById("pages");
25+
const checkElement = document.getElementById("check");
2726

2827
//check the right input from forms and if its ok -> add the new book (object in array)
2928
//via Book function and start render function
3029
function submit() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
30+
if (!titleElement.value || !authorElement.value || !pagesElement.value) {
3731
alert("Please fill all fields!");
3832
return false;
33+
} else if (!titleElement.value.match(/^(?=.*[a-zA-Z])[a-zA-Z ]+$/)) {
34+
alert("Please enter a valid book title!");
35+
return false;
36+
} else if (!authorElement.value.match(/^(?=.*[a-zA-Z])[a-zA-Z ]+$/)) {
37+
alert("Please enter a valid author name!");
38+
return false;
39+
} else if (!pagesElement.value.match(/^[0-9]+$/)) {
40+
alert("Please enter a valid number of pages!");
41+
return false;
3942
} else {
40-
let book = new Book(title.value, author.value, pages.value, check.checked);
43+
let book = new Book(
44+
titleElement.value,
45+
authorElement.value,
46+
pagesElement.value,
47+
checkElement.checked
48+
);
49+
4150
myLibrary.push(book);
4251
render();
4352
}
4453
}
4554

4655
function Book(title, author, pages, check) {
47-
this.title = title;
48-
this.author = author;
49-
this.pages = pages;
56+
this.title = title.trim(); //remove leading/trailing spaces
57+
this.author = author.trim();
58+
this.pages = Number(pages); // Ensure pages is a number
5059
this.check = check;
5160
}
5261

5362
function render() {
54-
let table = document.getElementById("display");
55-
let rowsNumber = table.rows.length;
63+
//Method 1: To clear the table
64+
//let table = document.getElementById("display");
65+
//delete all rows except the first one (header)
66+
//let rowsNumber = table.rows.length;
5667
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n--) {
58-
table.deleteRow(n);
59-
}
68+
//for (let n = rowsNumber - 1; n > 0; n--) {
69+
//table.deleteRow(n);
70+
//}
71+
72+
//Method 2: To clear the table in one operation
73+
//replaceChildren() is a modern method to clear all children of an element
74+
let tableBody = document.querySelector("tbody");
75+
tableBody.replaceChildren(); // Clear the table body
76+
6077
//insert updated row and cells
6178
let length = myLibrary.length;
6279
for (let i = 0; i < length; i++) {
63-
let row = table.insertRow(1);
80+
let row = tableBody.insertRow(0); // Insert a new row at the beginning of the table body
6481
let titleCell = row.insertCell(0);
6582
let authorCell = row.insertCell(1);
6683
let pagesCell = row.insertCell(2);
6784
let wasReadCell = row.insertCell(3);
6885
let deleteCell = row.insertCell(4);
69-
titleCell.innerHTML = myLibrary[i].title;
70-
authorCell.innerHTML = myLibrary[i].author;
71-
pagesCell.innerHTML = myLibrary[i].pages;
86+
87+
//innerHTML was replaced with textContent for cleaner text handling / code semantics.
88+
titleCell.textContent = myLibrary[i].title;
89+
authorCell.textContent = myLibrary[i].author;
90+
pagesCell.textContent = myLibrary[i].pages;
7291

7392
//add and wait for action for read/unread button
74-
let changeBut = document.createElement("button");
75-
changeBut.id = i;
76-
changeBut.className = "btn btn-success";
77-
wasReadCell.appendChild(changeBut);
93+
let readButton = document.createElement("button");
94+
95+
// readButton.id = i; /
96+
// No need for an ID here, as we can use the index directly
97+
readButton.className = "btn btn-success";
98+
wasReadCell.appendChild(readButton);
7899
let readStatus = "";
79100
if (myLibrary[i].check == false) {
80101
readStatus = "No";
81102
} else {
82103
readStatus = "Yes";
83104
}
84-
changeBut.innerText = readStatus;
105+
readButton.innerText = readStatus;
85106

86-
changeBut.addEventListener("click", function () {
107+
readButton.addEventListener("click", function () {
87108
myLibrary[i].check = !myLibrary[i].check;
88109
render();
89110
});
90111

91112
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
93-
delButton.id = i + 5;
94-
deleteCell.appendChild(delButton);
95-
delButton.className = "btn btn-warning";
96-
delButton.innerHTML = "Delete";
97-
delButton.addEventListener("click", function () {
98-
alert(`You've deleted title: ${myLibrary[i].title}`);
99-
myLibrary.splice(i, 1);
100-
render();
113+
let deleteButton = document.createElement("button");
114+
115+
// No need to set an ID for the delete button, as we can use the index directly
116+
//deleteButton.id = i + 5;
117+
deleteCell.appendChild(deleteButton);
118+
deleteButton.className = "btn btn-warning";
119+
deleteButton.textContent = "Delete";
120+
deleteButton.addEventListener("click", function () {
121+
const deletedTitle = myLibrary[i].title; //save the title of the book to be deleted
122+
myLibrary.splice(i, 1); // Remove the book from the library array
123+
render(); // update the table after deletion
124+
125+
// Use setTimeout to ensure the alert is shown after the render
126+
// This is useful to ensure the UI is updated before showing the alert
127+
setTimeout(() => {
128+
alert(`You've deleted title: ${deletedTitle}`);
129+
}, 0.5);
101130
});
102131
}
103132
}

0 commit comments

Comments
 (0)