Skip to content

Commit 9198e90

Browse files
committed
Book Library bugs fixed and Book Library working normal.
1 parent c89c066 commit 9198e90

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "chrome",
9+
"request": "launch",
10+
"name": "Launch Chrome against localhost",
11+
"url": "http://localhost:8080",
12+
"webRoot": "${workspaceFolder}"
13+
}
14+
]
15+
}

debugging/book-library/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ <h1>Library</h1>
9191
</tbody>
9292
</table>
9393

94-
<script src="script.js"></script>
94+
<script type="module" src="script.js"></script>
9595
</body>
9696
</html>

debugging/book-library/script.js

Lines changed: 57 additions & 17 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",
@@ -28,21 +28,56 @@ const check = document.getElementById("check");
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+
// Letters + spaces only (authors)
32+
const lettersOnly = /^[A-Za-z\s]+$/;
33+
34+
// Title: letters + numbers + spaces (no special characters)
35+
const titleAllowed = /^[A-Za-z0-9\s]+$/;
36+
37+
// Empty field check
3138
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
39+
title.value.trim() === "" ||
40+
author.value.trim() === "" ||
41+
pages.value.trim() === ""
3642
) {
3743
alert("Please fill all fields!");
3844
return false;
39-
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
42-
render();
4345
}
46+
47+
// Title validation
48+
if (!titleAllowed.test(title.value)) {
49+
alert("Title must contain only letters, numbers, and spaces!");
50+
return false;
51+
}
52+
53+
// Author validation
54+
if (!lettersOnly.test(author.value)) {
55+
alert("Author name must contain only letters!");
56+
return false;
57+
}
58+
59+
// Pages >= 1
60+
let pagesNum = Number(pages.value);
61+
62+
if (isNaN(pagesNum) || pagesNum < 1) {
63+
alert("Pages must be a number greater than or equal to 1!");
64+
return false;
65+
}
66+
67+
// Create and save the book
68+
let book = new Book(title.value, author.value, pages.value, check.checked);
69+
myLibrary.push(book);
70+
71+
// Clear inputs (optional)
72+
title.value = "";
73+
author.value = "";
74+
pages.value = "";
75+
check.checked = false;
76+
77+
render();
4478
}
4579

80+
4681
function Book(title, author, pages, check) {
4782
this.title = title;
4883
this.author = author;
@@ -53,8 +88,8 @@ function Book(title, author, pages, check) {
5388
function render() {
5489
let table = document.getElementById("display");
5590
let rowsNumber = table.rows.length;
56-
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
91+
// delete old table rows (keep header row 0)
92+
for (let n = rowsNumber - 1; n > 0; n--) {
5893
table.deleteRow(n);
5994
}
6095
//insert updated row and cells
@@ -72,11 +107,12 @@ function render() {
72107

73108
//add and wait for action for read/unread button
74109
let changeBut = document.createElement("button");
75-
changeBut.id = i;
110+
changeBut.id = "read-btn-" + i;
76111
changeBut.className = "btn btn-success";
77112
wasReadCell.appendChild(changeBut);
78113
let readStatus = "";
79-
if (myLibrary[i].check == false) {
114+
// show "Yes" when check is true (read), otherwise "No"
115+
if (myLibrary[i].check === true) {
80116
readStatus = "Yes";
81117
} else {
82118
readStatus = "No";
@@ -89,15 +125,19 @@ function render() {
89125
});
90126

91127
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
93-
delBut.id = i + 5;
94-
deleteCell.appendChild(delBut);
128+
let delBut = document.createElement("button");
129+
delBut.id = "del-btn-" + i;
95130
delBut.className = "btn btn-warning";
96131
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
132+
deleteCell.appendChild(delBut);
133+
134+
delBut.addEventListener("click", function () {
98135
alert(`You've deleted title: ${myLibrary[i].title}`);
99136
myLibrary.splice(i, 1);
100137
render();
101138
});
102139
}
103140
}
141+
142+
143+
// Book Library bugs fixed and Book Library working normal.

0 commit comments

Comments
 (0)