Skip to content

Commit 31fa600

Browse files
committed
Fix various issues in book library functionality and improve code quality
1 parent 82f218f commit 31fa600

1 file changed

Lines changed: 38 additions & 39 deletions

File tree

debugging/book-library/script.js

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
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("Robison Crusoe", "Daniel Defoe", "252", true);
9+
if (myLibrary.length === 0) {
10+
let book1 = new Book("Robinson 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
);
17+
1718
myLibrary.push(book1);
1819
myLibrary.push(book2);
19-
render();
2020
}
2121
}
2222

@@ -25,22 +25,20 @@ const author = document.getElementById("author");
2525
const pages = document.getElementById("pages");
2626
const check = document.getElementById("check");
2727

28-
//check the right input from forms and if its ok -> add the new book (object in array)
29-
//via Book function and start render function
28+
// Add a new book
3029
function submit() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
30+
if (!title.value || !author.value || !pages.value) {
3731
alert("Please fill all fields!");
38-
return false;
39-
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
42-
render();
32+
return;
4333
}
34+
35+
// FIXED: author was incorrectly set to title.value
36+
let book = new Book(title.value, author.value, pages.value, check.checked);
37+
38+
// FIXED: wrong variable name (library → myLibrary)
39+
myLibrary.push(book);
40+
41+
render();
4442
}
4543

4644
function Book(title, author, pages, check) {
@@ -53,51 +51,52 @@ function Book(title, author, pages, check) {
5351
function render() {
5452
let table = document.getElementById("display");
5553
let rowsNumber = table.rows.length;
56-
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
54+
55+
// FIXED: missing parenthesis
56+
for (let n = rowsNumber - 1; n > 0; n--) {
5857
table.deleteRow(n);
5958
}
60-
//insert updated row and cells
61-
let length = myLibrary.length;
62-
for (let i = 0; i < length; i++) {
59+
60+
for (let i = 0; i < myLibrary.length; i++) {
6361
let row = table.insertRow(1);
62+
6463
let titleCell = row.insertCell(0);
6564
let authorCell = row.insertCell(1);
6665
let pagesCell = row.insertCell(2);
6766
let wasReadCell = row.insertCell(3);
6867
let deleteCell = row.insertCell(4);
68+
6969
titleCell.innerHTML = myLibrary[i].title;
7070
authorCell.innerHTML = myLibrary[i].author;
7171
pagesCell.innerHTML = myLibrary[i].pages;
7272

73-
//add and wait for action for read/unread button
73+
// READ / UNREAD BUTTON
7474
let changeBut = document.createElement("button");
75-
changeBut.id = i;
7675
changeBut.className = "btn btn-success";
77-
wasReadCell.appendChild(changeBut);
78-
let readStatus = "";
79-
if (myLibrary[i].check == false) {
80-
readStatus = "Yes";
81-
} else {
82-
readStatus = "No";
83-
}
84-
changeBut.innerText = readStatus;
76+
77+
// FIXED: logic was reversed
78+
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
8579

8680
changeBut.addEventListener("click", function () {
8781
myLibrary[i].check = !myLibrary[i].check;
8882
render();
8983
});
9084

91-
//add delete button to every row and render again
85+
wasReadCell.appendChild(changeBut);
86+
87+
// DELETE BUTTON
9288
let delButton = document.createElement("button");
93-
delBut.id = i + 5;
94-
deleteCell.appendChild(delBut);
95-
delBut.className = "btn btn-warning";
96-
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
89+
delButton.className = "btn btn-warning";
90+
delButton.innerHTML = "Delete";
91+
92+
// FIXED: wrong variable name (delBut → delButton)
93+
// FIXED: wrong event name ("clicks" → "click")
94+
delButton.addEventListener("click", function () {
9895
alert(`You've deleted title: ${myLibrary[i].title}`);
9996
myLibrary.splice(i, 1);
10097
render();
10198
});
99+
100+
deleteCell.appendChild(delButton);
102101
}
103102
}

0 commit comments

Comments
 (0)