Skip to content

Commit 1b926cf

Browse files
committed
completed book-library debugging
1 parent 282246f commit 1b926cf

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

debugging/book-library/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ <h1>Library</h1>
5151
class="form-control"
5252
id="pages"
5353
name="pages"
54+
min="1"
5455
required
5556
/>
5657
<label class="form-check-label">
@@ -91,6 +92,6 @@ <h1>Library</h1>
9192
</tbody>
9293
</table>
9394

94-
<script src="script.js"></script>
95+
<script src="script.js" defer></script>
9596
</body>
9697
</html>

debugging/book-library/script.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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("Robinson Crusoe", "Daniel Defoe", "252", true);
1111
let book2 = new Book(
1212
"The Old Man and the Sea",
1313
"Ernest Hemingway",
@@ -29,20 +29,27 @@ const check = document.getElementById("check");
2929
//via Book function and start render function
3030
function submit() {
3131
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
32+
!title.value.trim() ||
33+
!author.value.trim() ||
34+
!pages.value
3635
) {
3736
alert("Please fill all fields!");
3837
return false;
3938
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
39+
let book = new Book(title.value, author.value, pages.value, check.checked);
40+
myLibrary.push(book);
41+
resetAddNewBook()
4242
render();
4343
}
4444
}
4545

46+
function resetAddNewBook() {
47+
title.value = '';
48+
author.value = '';
49+
pages.value = '';
50+
check.checked = false;
51+
}
52+
4653
function Book(title, author, pages, check) {
4754
this.title = title;
4855
this.author = author;
@@ -54,49 +61,49 @@ function render() {
5461
let table = document.getElementById("display");
5562
let rowsNumber = table.rows.length;
5663
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
64+
for (let n = rowsNumber - 1; n > 0; n-- ){
5865
table.deleteRow(n);
5966
}
6067
//insert updated row and cells
6168
let length = myLibrary.length;
62-
for (let i = 0; i < length; i++) {
63-
let row = table.insertRow(1);
69+
for (let libraryBookIndex = 0; libraryBookIndex < length; libraryBookIndex++) {
70+
let row = table.insertRow(-1);
6471
let titleCell = row.insertCell(0);
6572
let authorCell = row.insertCell(1);
6673
let pagesCell = row.insertCell(2);
6774
let wasReadCell = row.insertCell(3);
6875
let deleteCell = row.insertCell(4);
69-
titleCell.innerHTML = myLibrary[i].title;
70-
authorCell.innerHTML = myLibrary[i].author;
71-
pagesCell.innerHTML = myLibrary[i].pages;
76+
titleCell.innerHTML = myLibrary[libraryBookIndex].title;
77+
authorCell.innerHTML = myLibrary[libraryBookIndex].author;
78+
pagesCell.innerHTML = myLibrary[libraryBookIndex].pages;
7279

7380
//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);
81+
let readOrUnreadButton = document.createElement("button");
82+
readOrUnreadButton.id = `readOrUnreadBtn${libraryBookIndex}`;
83+
readOrUnreadButton.className = "btn btn-success";
84+
wasReadCell.appendChild(readOrUnreadButton);
7885
let readStatus = "";
79-
if (myLibrary[i].check == false) {
86+
if (myLibrary[libraryBookIndex].check) {
8087
readStatus = "Yes";
8188
} else {
8289
readStatus = "No";
8390
}
84-
changeBut.innerText = readStatus;
91+
readOrUnreadButton.innerText = readStatus;
8592

86-
changeBut.addEventListener("click", function () {
87-
myLibrary[i].check = !myLibrary[i].check;
93+
readOrUnreadButton.addEventListener("click", function () {
94+
myLibrary[libraryBookIndex].check = !myLibrary[libraryBookIndex].check;
8895
render();
8996
});
9097

9198
//add delete button to every row and render again
9299
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 () {
98-
alert(`You've deleted title: ${myLibrary[i].title}`);
99-
myLibrary.splice(i, 1);
100+
delButton.id = `delBtn${libraryBookIndex}`;
101+
deleteCell.appendChild(delButton);
102+
delButton.className = "btn btn-warning";
103+
delButton.innerHTML = "Delete";
104+
delButton.addEventListener("click", () => {
105+
alert(`You've deleted title: ${myLibrary[libraryBookIndex].title}`);
106+
myLibrary.splice(libraryBookIndex, 1);
100107
render();
101108
});
102109
}

0 commit comments

Comments
 (0)