Skip to content

Commit 333f269

Browse files
committed
made some modifications in the js file
1 parent b8cfc20 commit 333f269

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

debugging/book-library/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h1>Library</h1>
5858
type="button"
5959
value="Submit"
6060
class="btn btn-primary"
61-
onclick="addButton();"
61+
onclick="addBook();"
6262
>
6363
</div>
6464
</div>

debugging/book-library/script.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ 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
);
17-
myLibrary.push(book1);
18-
myLibrary.push(book2);
19-
render();
17+
myLibrary.push(book1,book2);
18+
2019
}
2120
}
2221

@@ -28,21 +27,20 @@ const check = document.getElementById("check");
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 addBook() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == "" ||
36-
author.value == null ||
37-
author.value == ""
38-
) {
39-
alert("Please fill all fields!");
30+
if (!title.value.trim() || !author.value.trim() || !pages.value.trim()) {
31+
alert("Please fill all fields!");
32+
return false;
33+
}
34+
35+
let pagesNumber = parseInt(pages.value, 10);
36+
if (isNaN(pagesNumber)) {
37+
alert("Page count must be a number!");
4038
return false;
41-
} else {
42-
let book = new Book(title.value, title.value, pages.value, check.checked);
43-
myLibrary.push(book);
44-
render();
4539
}
40+
let book = new Book(title.value.trim(), author.value.trim(), pagesNumber, check.checked);
41+
myLibrary.push(book);
42+
render();
43+
4644
}
4745

4846
function Book(title, author, pages, check) {
@@ -68,35 +66,36 @@ function render() {
6866
let pagesCell = row.insertCell(2);
6967
let wasReadCell = row.insertCell(3);
7068
let deleteCell = row.insertCell(4);
71-
titleCell.innerHTML = myLibrary[i].title;
72-
authorCell.innerHTML = myLibrary[i].author;
73-
pagesCell.innerHTML = myLibrary[i].pages;
69+
titleCell.textContent = myLibrary[i].title;
70+
authorCell.textContent = myLibrary[i].author;
71+
pagesCell.textContent = myLibrary[i].pages;
72+
7473

7574
//add and wait for action for read/unread button
76-
let changeBut = document.createElement("button");
77-
changeBut.id = i;
78-
changeBut.className = "btn btn-success";
79-
wasReadCell.appendChild(changeBut);
75+
let toggleReadBtn = document.createElement("button");
76+
toggleReadBtn.dataset.index = i;
77+
toggleReadBtn.className = "btn btn-success";
78+
wasReadCell.appendChild(toggleReadBtn);
8079
let readStatus = "";
8180
if (myLibrary[i].check == true) {
8281
readStatus = "Yes";
8382
} else {
8483
readStatus = "No";
8584
}
86-
changeBut.innerText = readStatus;
85+
toggleReadBtn.innerText = readStatus;
8786

88-
changeBut.addEventListener("click", function () {
87+
toggleReadBtn.addEventListener("click", function () {
8988
myLibrary[i].check = !myLibrary[i].check;
9089
render();
9190
});
9291

9392
//add delete button to every row and render again
94-
let delBut = document.createElement("button");
95-
delBut.id = i + 5;
96-
deleteCell.appendChild(delBut);
97-
delBut.className = "btn btn-warning";
98-
delBut.innerHTML = "Delete";
99-
delBut.addEventListener("click", function () {
93+
let deleteBtn = document.createElement("button");
94+
deleteBtn.dataset.index = i;
95+
deleteCell.appendChild(deleteBtn);
96+
deleteBtn.className = "btn btn-warning";
97+
deleteBtn.innerHTML = "Delete";
98+
deleteBtn.addEventListener("click", function () {
10099
alert(`You've deleted title: ${myLibrary[i].title}`);
101100
myLibrary.splice(i, 1);
102101
render();

0 commit comments

Comments
 (0)