Skip to content

Commit 75de4c4

Browse files
committed
refactor: apply reviewer feedback for performance, clarity, and UX improvements
1 parent 558b525 commit 75de4c4

1 file changed

Lines changed: 20 additions & 39 deletions

File tree

debugging/book-library/script.js

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
let myLibrary = [];
22

3-
window.addEventListener("load", function (e) {
3+
window.addEventListener("load", function () {
44
populateStorage();
55
render();
66
});
77

88
function populateStorage() {
9-
if (myLibrary.length == 0) {
10-
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
11-
let book2 = new Book(
12-
"The Old Man and the Sea",
13-
"Ernest Hemingway",
14-
127,
15-
true
16-
);
9+
if (myLibrary.length === 0) {
10+
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
11+
let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true);
1712
myLibrary.push(book1);
1813
myLibrary.push(book2);
1914
render();
@@ -25,39 +20,31 @@ const author = document.getElementById("author");
2520
const pages = document.getElementById("pages");
2621
const check = document.getElementById("check");
2722

28-
2923
function submit() {
30-
31-
if (
32-
!title.value?.trim() ||
33-
!author.value?.trim() ||
34-
!pages.value?.trim()
35-
) {
24+
const titleInput = title.value.trim();
25+
const authorInput = author.value.trim();
26+
const pagesInput = pages.value.trim();
27+
const pagesNum = Number(pagesInput);
28+
29+
if (!titleInput || !authorInput || !pagesInput) {
3630
alert("Please fill all fields!");
3731
return false;
3832
}
3933

40-
const pagesNum = Number(pages.value);
4134
if (!Number.isInteger(pagesNum) || pagesNum < 1) {
4235
alert("Pages must be a positive whole number.");
4336
return false;
4437
}
4538

46-
const book = new Book(
47-
title.value.trim(),
48-
author.value.trim(),
49-
pagesNum,
50-
check.checked
51-
);
39+
const book = new Book(titleInput, authorInput, pagesNum, check.checked);
5240
myLibrary.push(book);
5341
render();
54-
55-
42+
5643
title.value = "";
5744
author.value = "";
5845
pages.value = "";
5946
check.checked = false;
60-
47+
6148
return true;
6249
}
6350

@@ -70,11 +57,7 @@ function Book(title, author, pages, check) {
7057

7158
function render() {
7259
let table = document.getElementById("display");
73-
let rowsNumber = table.rows.length;
74-
75-
for (let n = rowsNumber - 1; n > 0; n--) {
76-
table.deleteRow(n);
77-
}
60+
table.querySelector("tbody").innerHTML = "";
7861

7962
let length = myLibrary.length;
8063
for (let i = 0; i < length; i++) {
@@ -88,29 +71,27 @@ function render() {
8871
authorCell.textContent = myLibrary[i].author;
8972
pagesCell.textContent = String(myLibrary[i].pages);
9073

91-
9274
let changeBut = document.createElement("button");
93-
changeBut.id = i;
9475
changeBut.className = "btn btn-success";
95-
wasReadCell.appendChild(changeBut);
96-
9776
changeBut.textContent = myLibrary[i].check ? "Yes" : "No";
77+
wasReadCell.appendChild(changeBut);
9878

9979
changeBut.addEventListener("click", function () {
10080
myLibrary[i].check = !myLibrary[i].check;
10181
render();
10282
});
10383

10484
const delBut = document.createElement("button");
105-
delBut.id = String(i + 5);
10685
delBut.className = "btn btn-warning";
10786
delBut.textContent = "Delete";
87+
deleteCell.appendChild(delBut);
88+
10889
delBut.addEventListener("click", function () {
109-
alert(`You've deleted title: ${myLibrary[i].title}`);
90+
const deletedTitle = myLibrary[i].title;
11091
myLibrary.splice(i, 1);
11192
render();
112-
});
113-
deleteCell.appendChild(delBut);
93+
alert(`You've deleted title: ${deletedTitle}`);
94+
});
11495
}
11596
}
11697

0 commit comments

Comments
 (0)