Skip to content

Commit 90bc528

Browse files
committed
Debugging task - fixed bugs in library app (render, add, delete, read toggle)
1 parent 3a422e9 commit 90bc528

2 files changed

Lines changed: 67 additions & 63 deletions

File tree

debugging/book-library/index.html

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title> </title>
5-
<meta
6-
charset="utf-8"
7-
name="viewport"
8-
content="width=device-width, initial-scale=1.0"
9-
/>
4+
<title>Library App</title>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
8+
<!-- Bootstrap -->
109
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
1110
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
1211
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
1312
<link
1413
rel="stylesheet"
1514
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
1615
/>
16+
1717
<link rel="stylesheet" type="text/css" href="style.css" />
1818
</head>
1919

@@ -23,28 +23,32 @@ <h1>Library</h1>
2323
<p>Add books to your virtual library</p>
2424
</div>
2525

26+
<!-- Toggle Form -->
2627
<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
2728
Add new book
2829
</button>
2930

31+
<!-- Form -->
3032
<div id="demo" class="collapse">
3133
<div class="form-group">
3234
<label for="title">Title:</label>
3335
<input
34-
type="title"
36+
type="text"
3537
class="form-control"
3638
id="title"
3739
name="title"
3840
required
3941
/>
40-
<label for="author">Author: </label>
42+
43+
<label for="author">Author:</label>
4144
<input
42-
type="author"
45+
type="text"
4346
class="form-control"
4447
id="author"
4548
name="author"
4649
required
4750
/>
51+
4852
<label for="pages">Pages:</label>
4953
<input
5054
type="number"
@@ -53,23 +57,23 @@ <h1>Library</h1>
5357
name="pages"
5458
required
5559
/>
60+
5661
<label class="form-check-label">
5762
<input
5863
type="checkbox"
5964
class="form-check-input"
6065
id="check"
61-
value=""
62-
/>Read
66+
/>
67+
Read
6368
</label>
64-
<input
65-
type="submit"
66-
value="Submit"
67-
class="btn btn-primary"
68-
onclick="submit();"
69-
/>
69+
70+
<button class="btn btn-primary mt-2" onclick="submit()">
71+
Submit
72+
</button>
7073
</div>
7174
</div>
7275

76+
<!-- Table -->
7377
<table class="table" id="display">
7478
<thead class="thead-dark">
7579
<tr>
@@ -81,16 +85,11 @@ <h1>Library</h1>
8185
</tr>
8286
</thead>
8387
<tbody>
84-
<tr>
85-
<td></td>
86-
<td></td>
87-
<td></td>
88-
<td></td>
89-
<td></td>
90-
</tr>
88+
<!-- JS will populate rows here -->
9189
</tbody>
9290
</table>
9391

92+
<!-- Scripts -->
9493
<script src="script.js"></script>
9594
</body>
96-
</html>
95+
</html>

debugging/book-library/script.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,108 @@
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",
1414
"127",
1515
true
1616
);
17-
myLibrary.push(book1);
18-
myLibrary.push(book2);
19-
render();
17+
myLibrary.push(book1, book2);
2018
}
2119
}
2220

21+
// FORM INPUTS
2322
const title = document.getElementById("title");
2423
const author = document.getElementById("author");
2524
const pages = document.getElementById("pages");
2625
const check = document.getElementById("check");
2726

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
27+
// ADD BOOK
3028
function submit() {
3129
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
30+
title.value === "" ||
31+
author.value === "" ||
32+
pages.value === ""
3633
) {
3734
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();
35+
return;
4336
}
37+
38+
let book = new Book(
39+
title.value,
40+
author.value, // ✅ FIXED (was using title twice)
41+
pages.value,
42+
check.checked // ✅ FIXED (correct checkbox value)
43+
);
44+
45+
myLibrary.push(book); // ✅ FIXED (was "library")
46+
render();
4447
}
4548

49+
// BOOK OBJECT
4650
function Book(title, author, pages, check) {
4751
this.title = title;
4852
this.author = author;
4953
this.pages = pages;
5054
this.check = check;
5155
}
5256

57+
// RENDER TABLE
5358
function render() {
5459
let table = document.getElementById("display");
60+
61+
// CLEAR OLD ROWS
5562
let rowsNumber = table.rows.length;
56-
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
63+
for (let n = rowsNumber - 1; n > 0; n--) { // ✅ FIXED SYNTAX
5864
table.deleteRow(n);
5965
}
60-
//insert updated row and cells
61-
let length = myLibrary.length;
62-
for (let i = 0; i < length; i++) {
66+
67+
// ADD ROWS
68+
for (let i = 0; i < myLibrary.length; i++) {
6369
let row = table.insertRow(1);
70+
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);
76+
6977
titleCell.innerHTML = myLibrary[i].title;
7078
authorCell.innerHTML = myLibrary[i].author;
7179
pagesCell.innerHTML = myLibrary[i].pages;
7280

73-
//add and wait for action for read/unread button
81+
// READ BUTTON
7482
let changeBut = document.createElement("button");
75-
changeBut.id = i;
7683
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+
85+
let readStatus = myLibrary[i].check ? "Yes" : "No"; // ✅ FIXED
8486
changeBut.innerText = readStatus;
8587

8688
changeBut.addEventListener("click", function () {
8789
myLibrary[i].check = !myLibrary[i].check;
8890
render();
8991
});
9092

91-
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
93-
delBut.id = i + 5;
94-
deleteCell.appendChild(delBut);
93+
wasReadCell.appendChild(changeBut);
94+
95+
// DELETE BUTTON
96+
let delBut = document.createElement("button"); // ✅ FIXED NAME
9597
delBut.className = "btn btn-warning";
9698
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
98-
alert(`You've deleted title: ${myLibrary[i].title}`);
99+
100+
delBut.addEventListener("click", function () { // ✅ FIXED EVENT
101+
alert(`You've deleted: ${myLibrary[i].title}`);
99102
myLibrary.splice(i, 1);
100103
render();
101104
});
105+
106+
deleteCell.appendChild(delBut);
102107
}
103-
}
108+
}

0 commit comments

Comments
 (0)