Skip to content

Commit fee43ee

Browse files
committed
Refactor HTML structure, enhance form validation, and improve JavaScript functionality for book library application & complete code reading tasks
1 parent 282246f commit fee43ee

File tree

5 files changed

+77
-58
lines changed

5 files changed

+77
-58
lines changed

debugging/book-library/index.html

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="en-GB">
33
<head>
4-
<title> </title>
4+
<title>My VirtualLibrary</title>
55
<meta
66
charset="utf-8"
77
name="viewport"
88
content="width=device-width, initial-scale=1.0"
99
/>
10+
<meta name="description" content="A simple book library application" />
1011
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
1112
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
1213
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
@@ -23,23 +24,23 @@ <h1>Library</h1>
2324
<p>Add books to your virtual library</p>
2425
</div>
2526

26-
<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
27+
<button type="button" data-toggle="collapse" data-target="#demo" class="btn btn-primary">
2728
Add new book
2829
</button>
2930

3031
<div id="demo" class="collapse">
31-
<div class="form-group">
32+
<form class="form-group">
3233
<label for="title">Title:</label>
3334
<input
34-
type="title"
35+
type="text"
3536
class="form-control"
3637
id="title"
3738
name="title"
3839
required
3940
/>
4041
<label for="author">Author: </label>
4142
<input
42-
type="author"
43+
type="text"
4344
class="form-control"
4445
id="author"
4546
name="author"
@@ -51,23 +52,22 @@ <h1>Library</h1>
5152
class="form-control"
5253
id="pages"
5354
name="pages"
55+
min="1"
5456
required
5557
/>
56-
<label class="form-check-label">
58+
<label class="form-check-label" for="check">
5759
<input
5860
type="checkbox"
5961
class="form-check-input"
6062
id="check"
61-
value=""
6263
/>Read
6364
</label>
64-
<input
65-
type="submit"
66-
value="Submit"
65+
<button
66+
id="add-book-btn"
67+
type="button"
6768
class="btn btn-primary"
68-
onclick="submit();"
69-
/>
70-
</div>
69+
> Add Book </button>
70+
</form>
7171
</div>
7272

7373
<table class="table" id="display">
@@ -77,7 +77,7 @@ <h1>Library</h1>
7777
<th>Author</th>
7878
<th>Number of Pages</th>
7979
<th>Read</th>
80-
<th></th>
80+
<th>Delete</th>
8181
</tr>
8282
</thead>
8383
<tbody>

debugging/book-library/readme.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ My website should be able to:
1212

1313
## Bugs to be fixed
1414

15-
1. Website loads but doesn't show any books
16-
2. Error in console when you try to add a book
17-
3. It uses the title name as the author name
18-
4. Delete button is broken
19-
5. When I add a book that I say I've read - it saves the wrong answer
15+
1. Website loads but doesn't show any books - fixed
16+
2. Error in console when you try to add a book - fixed
17+
3. It uses the title name as the author name - fixed
18+
4. Delete button is broken - fixed
19+
5. When I add a book that I say I've read - it saves the wrong answer - fixed
2020

2121
I think there are other some other small bugs in my code...but I'm lazy so I can't fix them all.
22+
- unsure if ive caught all the bugs but have made changes I thought were pertinent and ensure full lighthouse score
2223

2324
I wish somebody would help me!

debugging/book-library/script.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ let myLibrary = [];
22

33
window.addEventListener("load", function (e) {
44
populateStorage();
5-
render();
5+
const form = document.getElementById("bookForm");
6+
form?.addEventListener("submit", e => {
7+
e.preventDefault();
8+
});
9+
const addBookBtn = document.getElementById("addBookBtn");
10+
addBookBtn?.addEventListener("click", addBook);
611
});
712

813
function populateStorage() {
9-
if (myLibrary.length == 0) {
14+
if (myLibrary.length === 0) {
1015
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
1116
let book2 = new Book(
1217
"The Old Man and the Sea",
@@ -20,25 +25,24 @@ function populateStorage() {
2025
}
2126
}
2227

23-
const title = document.getElementById("title");
24-
const author = document.getElementById("author");
25-
const pages = document.getElementById("pages");
26-
const check = document.getElementById("check");
2728

2829
//check the right input from forms and if its ok -> add the new book (object in array)
2930
//via Book function and start render function
30-
function submit() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
31+
function addBook() {
32+
const title = document.getElementById("title");
33+
const author = document.getElementById("author");
34+
const pages = document.getElementById("pages");
35+
const check = document.getElementById("check");
36+
if (!title.value || !author.value || !pages.value) {
3737
alert("Please fill all fields!");
38-
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+
title.value = "";
42+
author.value = "";
43+
pages.value = "";
44+
check.checked = false;
45+
alert (`You've added ${book.title} to your library.`);
4246
render();
4347
}
4448
}
@@ -54,7 +58,7 @@ function render() {
5458
let table = document.getElementById("display");
5559
let rowsNumber = table.rows.length;
5660
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
61+
for (let n = rowsNumber - 1; n > 0; n--) {
5862
table.deleteRow(n);
5963
}
6064
//insert updated row and cells
@@ -66,22 +70,17 @@ function render() {
6670
let pagesCell = row.insertCell(2);
6771
let wasReadCell = row.insertCell(3);
6872
let deleteCell = row.insertCell(4);
69-
titleCell.innerHTML = myLibrary[i].title;
70-
authorCell.innerHTML = myLibrary[i].author;
71-
pagesCell.innerHTML = myLibrary[i].pages;
73+
titleCell.innerText = myLibrary[i].title;
74+
authorCell.innerText = myLibrary[i].author;
75+
pagesCell.innerText = myLibrary[i].pages;
7276

7377
//add and wait for action for read/unread button
7478
let changeBut = document.createElement("button");
75-
changeBut.id = i;
76-
changeBut.className = "btn btn-success";
79+
changeBut.type = "button";
7780
wasReadCell.appendChild(changeBut);
78-
let readStatus = "";
79-
if (myLibrary[i].check == false) {
80-
readStatus = "Yes";
81-
} else {
82-
readStatus = "No";
83-
}
81+
let readStatus = myLibrary[i].check ? "Yes" : "No";
8482
changeBut.innerText = readStatus;
83+
changeBut.className = 'btn '+ (myLibrary[i].check ? 'btn-success' : 'btn-danger');
8584

8685
changeBut.addEventListener("click", function () {
8786
myLibrary[i].check = !myLibrary[i].check;
@@ -90,12 +89,12 @@ function render() {
9089

9190
//add delete button to every row and render again
9291
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}`);
92+
delButton.type = "button";
93+
deleteCell.appendChild(delButton);
94+
delButton.className = "btn btn-danger";
95+
delButton.innerText = "Delete";
96+
delButton.addEventListener("click", function () {
97+
alert(`You've deleted ${myLibrary[i].title} from your library.`);
9998
myLibrary.splice(i, 1);
10099
render();
101100
});

debugging/book-library/style.css

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1+
.collapse.show{
2+
display: flex;
3+
}
4+
15
.form-group {
26
width: 400px;
37
height: 300px;
4-
align-self: left;
8+
align-self: flex-start;
59
padding-left: 20px;
610
}
711

812
.btn {
9-
display: block;
13+
font-weight: 600;
14+
color: black;
1015
}
1116

1217
.form-check-label {
1318
padding-left: 20px;
14-
margin: 5px 0px 5px 0px;
19+
padding-top: 10px;
20+
margin: 5px ;
1521
}
1622

17-
button.btn-info {
23+
button[data-toggle="collapse"] {
1824
margin: 20px;
1925
}
26+
27+
#add-book-btn {
28+
display: block;
29+
margin-top: 10px;
30+
}

debugging/code-reading/readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Take a look at the following code:
1717

1818
Explain why line 5 and line 8 output different numbers.
1919

20+
Due to x holding different values in global scope and functional scope
21+
2022
## Question 2
2123

2224
Take a look at the following code:
@@ -35,6 +37,9 @@ console.log(y);
3537

3638
What will be the output of this code. Explain your answer in 50 words or less.
3739

40+
The first console.log will log 10 to the console, while the second will log undefined and throw a reference error due to y only being available in the function
41+
42+
3843
## Question 3
3944

4045
Take a look at the following code:
@@ -62,3 +67,6 @@ console.log(y);
6267
```
6368

6469
What will be the output of this code. Explain your answer in 50 words or less.
70+
71+
First console.log outputs 9 as x is passed and the f1 call doesn't alter its value outside the fn.
72+
Second console.log prints the object {x:10} as f2 increments y.x by 1

0 commit comments

Comments
 (0)