Skip to content

Commit 6564055

Browse files
committed
fixed the bugs
1 parent 9d43e78 commit 6564055

2 files changed

Lines changed: 101 additions & 129 deletions

File tree

debugging/book-library/index.html

Lines changed: 57 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,64 @@
11
<!DOCTYPE html>
2-
<html>
3-
<head>
4-
<title> </title>
5-
<meta
6-
charset="utf-8"
7-
name="viewport"
8-
content="width=device-width, initial-scale=1.0"
9-
/>
10-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
11-
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
12-
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
13-
<link
14-
rel="stylesheet"
15-
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
16-
/>
17-
<link rel="stylesheet" type="text/css" href="style.css" />
18-
</head>
2+
<html lang="en">
193

20-
<body>
21-
<div class="jumbotron text-center">
22-
<h1>Library</h1>
23-
<p>Add books to your virtual library</p>
24-
</div>
4+
<head>
5+
<title>My Virtual Book Library </title>
6+
<meta charset="utf-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
9+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
10+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
11+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
12+
<link rel="stylesheet" type="text/css" href="style.css">
13+
</head>
14+
15+
<body>
16+
<div class="jumbotron text-center">
17+
<h1>Library</h1>
18+
<p>Add books to your virtual library</p>
19+
</div>
2520

26-
<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
27-
Add new book
28-
</button>
21+
<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
22+
Add new book
23+
</button>
2924

30-
<div id="demo" class="collapse">
31-
<div class="form-group">
32-
<label for="title">Title:</label>
33-
<input
34-
type="title"
35-
class="form-control"
36-
id="title"
37-
name="title"
38-
required
39-
/>
40-
<label for="author">Author: </label>
41-
<input
42-
type="author"
43-
class="form-control"
44-
id="author"
45-
name="author"
46-
required
47-
/>
48-
<label for="pages">Pages:</label>
49-
<input
50-
type="number"
51-
class="form-control"
52-
id="pages"
53-
name="pages"
54-
required
55-
/>
56-
<label class="form-check-label">
57-
<input
58-
type="checkbox"
59-
class="form-check-input"
60-
id="check"
61-
value=""
62-
/>Read
63-
</label>
64-
<input
65-
type="submit"
66-
value="Submit"
67-
class="btn btn-primary"
68-
onclick="submit();"
69-
/>
70-
</div>
25+
<div id="demo" class="collapse">
26+
<div class="form-group">
27+
<label for="title">Title:</label>
28+
<input type="text" class="form-control" id="title" name="title" required>
29+
<label for="author">Author: </label>
30+
<input type="text" class="form-control" id="author" name="author" required>
31+
<label for="pages">Pages:</label>
32+
<input type="number" class="form-control" id="pages" name="pages" required>
33+
<label class="form-check-label">
34+
<input type="checkbox" class="form-check-input" id="check" value="">Read
35+
</label>
36+
<input type="submit" value="Submit" class="btn btn-primary" onclick="submit();">
7137
</div>
38+
</div>
39+
40+
<table class="table" id="display">
41+
<thead class="thead-dark">
42+
<tr>
43+
<th>Title</th>
44+
<th>Author</th>
45+
<th>Number of Pages</th>
46+
<th>Read</th>
47+
<th></th>
48+
</tr>
49+
</thead>
50+
<tbody>
51+
<tr>
52+
<td></td>
53+
<td></td>
54+
<td></td>
55+
<td></td>
56+
<td></td>
57+
</tr>
58+
</tbody>
59+
</table>
7260

73-
<table class="table" id="display">
74-
<thead class="thead-dark">
75-
<tr>
76-
<th>Title</th>
77-
<th>Author</th>
78-
<th>Number of Pages</th>
79-
<th>Read</th>
80-
<th></th>
81-
</tr>
82-
</thead>
83-
<tbody>
84-
<tr>
85-
<td></td>
86-
<td></td>
87-
<td></td>
88-
<td></td>
89-
<td></td>
90-
</tr>
91-
</tbody>
92-
</table>
61+
<script src="script.js"></script>
62+
</body>
9363

94-
<script src="script.js"></script>
95-
</body>
96-
</html>
64+
</html>

debugging/book-library/script.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let myLibrary = [];
1+
const myLibrary = [];
22

33
window.addEventListener("load", function (e) {
44
populateStorage();
@@ -7,11 +7,11 @@ 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);
11-
let book2 = new Book(
10+
const book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
11+
const book2 = new Book(
1212
"The Old Man and the Sea",
1313
"Ernest Hemingway",
14-
"127",
14+
127,
1515
true
1616
);
1717
myLibrary.push(book1);
@@ -20,25 +20,34 @@ function populateStorage() {
2020
}
2121
}
2222

23-
const title = document.getElementById("title");
24-
const author = document.getElementById("author");
25-
const pages = document.getElementById("pages");
26-
const check = document.getElementById("check");
23+
const titleInput = document.getElementById("title");
24+
const authorInput = document.getElementById("author");
25+
const pagesInput = document.getElementById("pages");
26+
const readCheckBox = document.getElementById("check");
2727

2828
//check the right input from forms and if its ok -> add the new book (object in array)
2929
//via Book function and start render function
3030
function submit() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
31+
const trimmedTitle = titleInput.value.trim();
32+
const trimmedAuthor = authorInput.value.trim();
33+
const trimmedPages = +pagesInput.value.trim();
34+
35+
if (!trimmedTitle || !trimmedAuthor || !trimmedPages) {
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(
40+
trimmedTitle,
41+
trimmedAuthor,
42+
trimmedPages,
43+
readCheckbox.checked
44+
);
45+
myLibrary.push(book);
46+
47+
titleInput.value = "";
48+
authorInput.value = "";
49+
pagesInput.value = "";
50+
readCheckbox.checked = false;
4251
render();
4352
}
4453
}
@@ -51,53 +60,48 @@ function Book(title, author, pages, check) {
5160
}
5261

5362
function render() {
54-
let table = document.getElementById("display");
55-
let rowsNumber = table.rows.length;
63+
const table = document.getElementById("display");
64+
const rowsNumber = table.rows.length;
5665
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
66+
for (let n = rowsNumber - 1; n > 0; n--) {
5867
table.deleteRow(n);
5968
}
6069
//insert updated row and cells
61-
let length = myLibrary.length;
70+
const length = myLibrary.length;
6271
for (let i = 0; i < length; i++) {
63-
let row = table.insertRow(1);
64-
let titleCell = row.insertCell(0);
65-
let authorCell = row.insertCell(1);
66-
let pagesCell = row.insertCell(2);
67-
let wasReadCell = row.insertCell(3);
68-
let deleteCell = row.insertCell(4);
69-
titleCell.innerHTML = myLibrary[i].title;
70-
authorCell.innerHTML = myLibrary[i].author;
71-
pagesCell.innerHTML = myLibrary[i].pages;
72+
const row = table.insertRow(1);
73+
const titleCell = row.insertCell(0);
74+
const authorCell = row.insertCell(1);
75+
const pagesCell = row.insertCell(2);
76+
const wasReadCell = row.insertCell(3);
77+
const deleteCell = row.insertCell(4);
78+
titleCell.textContent = myLibrary[i].title;
79+
authorCell.textContent = myLibrary[i].author;
80+
pagesCell.textContent = myLibrary[i].pages;
7281

7382
//add and wait for action for read/unread button
74-
let changeBut = document.createElement("button");
83+
const changeBut = document.createElement("button");
7584
changeBut.id = i;
7685
changeBut.className = "btn btn-success";
7786
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;
87+
changeBut.innerHTML = myLibrary[i].check ? "Yes" : "No";
8588

8689
changeBut.addEventListener("click", function () {
8790
myLibrary[i].check = !myLibrary[i].check;
8891
render();
8992
});
9093

9194
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
95+
const delButton = document.createElement("button");
9396
delBut.id = i + 5;
9497
deleteCell.appendChild(delBut);
9598
delBut.className = "btn btn-warning";
9699
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
98-
alert(`You've deleted title: ${myLibrary[i].title}`);
100+
delBut.addEventListener("click", function () {
101+
const deletedBook = myLibrary[i];
99102
myLibrary.splice(i, 1);
100103
render();
104+
alert(`You've deleted title: ${deletedBook.title}`);
101105
});
102106
}
103107
}

0 commit comments

Comments
 (0)