Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fee43ee
Refactor HTML structure, enhance form validation, and improve JavaScr…
i786m Nov 25, 2025
2f35ce1
Merge branch 'CodeYourFuture:main' into coursework/booklibrary
i786m Dec 6, 2025
2648f54
fix meta tag errors in html
i786m Dec 8, 2025
bcf8c09
update script tag to use module type
i786m Dec 8, 2025
f95fbc6
fix ID for add book button in event listener
i786m Dec 8, 2025
9304166
use querySelector to target form tag
i786m Dec 8, 2025
df6e09d
convert pages value to a number when adding a book
i786m Dec 8, 2025
778d81d
change variable names for input elements to reflect what they store
i786m Dec 8, 2025
aea2a43
fix typo in book1
i786m Dec 8, 2025
58798b5
normalize, sanitize and validate data on input
i786m Dec 8, 2025
2a58ad6
refactor to delete rows in one operation
i786m Dec 8, 2025
8ac46e3
fix delete message timing issue
i786m Dec 8, 2025
5691196
use textContent instead of innerText for rendering book details
i786m Dec 8, 2025
f6a4060
improve button naming and clarity
i786m Dec 8, 2025
d9acf45
fix page numbers in populateStorage function to use numbers
i786m Dec 8, 2025
c6bf51d
refactor addBook and render functions
i786m Dec 8, 2025
e6143cb
use textContent instead of innerText for buttons
i786m Dec 8, 2025
ea49aa2
removed trailing slash on void elements
i786m Dec 8, 2025
f2a5fcf
remove empty table row in book display
i786m Dec 8, 2025
620b93c
refactor render to protect against null tbody
i786m Dec 8, 2025
0b76c33
format button label for better readability and update button styles
i786m Dec 8, 2025
d349fa1
update button colors for accessibility
i786m Dec 8, 2025
cbcd993
tidy up
i786m Dec 8, 2025
7f03371
add button color for improved visibility
i786m Dec 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 61 additions & 90 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,96 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<html lang="en-GB">

<body>
<div class="jumbotron text-center">
<h1>Library</h1>
<p>Add books to your virtual library</p>
</div>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A simple book library application">
<title>My Virtual Library</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
Add new book
</button>
<body>
<div class="jumbotron text-center">
<h1>Library</h1>
<p>Add books to your virtual library</p>
</div>

<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>
<input
type="author"
class="form-control"
id="author"
name="author"
required
/>
<label for="pages">Pages:</label>
<input
type="number"
class="form-control"
id="pages"
name="pages"
required
/>
<label class="form-check-label">
<input
type="checkbox"
class="form-check-input"
id="check"
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
</div>
</div>
<button type="button" data-toggle="collapse" data-target="#demo"
class="btn btn-primary">
Add new book
</button>

<table class="table" id="display">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Author</th>
<th>Number of Pages</th>
<th>Read</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<div id="demo" class="collapse">
<form class="form-group">
<label for="title">Title:</label>
<input type="text" class="form-control" id="title" name="title" required>
<label for="author">Author: </label>
<input type="text" class="form-control" id="author" name="author"
required>
<label for="pages">Pages:</label>
<input type="number" class="form-control" id="pages" name="pages" min="1"
required>
<label class="form-check-label" for="check">
<input type="checkbox" class="form-check-input" id="check">Read
</label>
<button id="add-book-btn" type="button" class="btn btn-primary">
Add Book
</button>
</form>
</div>

<script src="script.js"></script>
</body>
</html>
<table class="table" id="display">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Author</th>
<th>Number of Pages</th>
<th>Read</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

<script src="script.js" type="module"></script>
</body>

</html>
11 changes: 6 additions & 5 deletions debugging/book-library/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ My website should be able to:

## Bugs to be fixed

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

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

I wish somebody would help me!
111 changes: 55 additions & 56 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ let myLibrary = [];

window.addEventListener("load", function (e) {
populateStorage();
render();
const form = document.querySelector('form');
form?.addEventListener("submit", e => {
e.preventDefault();
});
const addBookBtn = document.getElementById("add-book-btn");
addBookBtn?.addEventListener("click", addBook);
});

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
if (myLibrary.length === 0) {
let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"127",
127,
true
);
myLibrary.push(book1);
Expand All @@ -20,27 +25,30 @@ function populateStorage() {
}
}

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");

//check the right input from forms and if its ok -> add the new book (object in array)
//via Book function and start render function
function submit() {
if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
function addBook() {
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const checkInput = document.getElementById("check");
const titleValue = titleInput.value.trim();
const authorValue = authorInput.value.trim();
const pagesValue = Number(pagesInput.value);
if (!titleValue || !authorValue) {
alert("Please fill in all the fields!");
return;
}
if (pagesValue < 1 || !Number.isInteger(pagesValue)) {
alert("Pages must be a positive integer!");
return;
}
let book = new Book(titleValue, authorValue, pagesValue, checkInput.checked);
myLibrary.push(book);
titleInput.value = "";
authorInput.value = "";
pagesInput.value = "";
checkInput.checked = false;
alert(`You've added ${book.title} to your library.`);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note:

alert() is not a friendly way to display messages in an app, especially non-critical messages.
It is ok to use alert() in this "exercise" app, just don't make it a habit.

Also, alert() can block the execution of the application. For example, even if you moved alert0 after render(), the rendering will still be blocked by alert(). So in both "addition" and "deletion" (lines 97-99), the updated list of books will only be rendered after the user closed the dialog box.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking into Bootstrap's alert classes and realised I could have in-page messages as opposed to alerts. I will implement this as a workaround.

render();
}

function Book(title, author, pages, check) {
Expand All @@ -52,52 +60,43 @@ function Book(title, author, pages, check) {

function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
table.deleteRow(n);
}
//insert updated row and cells
const tbody = table.querySelector('tbody');
if (!tbody) return;
tbody.innerHTML = '';
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = tbody.insertRow();
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
let wasReadCell = row.insertCell(3);
let deleteCell = row.insertCell(4);
titleCell.innerHTML = myLibrary[i].title;
authorCell.innerHTML = myLibrary[i].author;
pagesCell.innerHTML = myLibrary[i].pages;

//add and wait for action for read/unread button
let changeBut = document.createElement("button");
changeBut.id = i;
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;
titleCell.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = myLibrary[i].pages;
let toggleReadButton = document.createElement("button");
toggleReadButton.type = "button";
wasReadCell.appendChild(toggleReadButton);
let readStatus = myLibrary[i].check ? "Yes" : "No";
toggleReadButton.textContent = readStatus;
toggleReadButton.className = 'btn ' + (myLibrary[i].check ? 'btn-success' : 'btn-danger');

changeBut.addEventListener("click", function () {
toggleReadButton.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});

//add delete button to every row and render again
let delButton = document.createElement("button");
delBut.id = i + 5;
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);

let deleteButton = document.createElement("button");
deleteButton.type = "button";
deleteCell.appendChild(deleteButton);
deleteButton.className = "btn btn-danger";
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", function () {
const deletedTitle = myLibrary[i].title;
myLibrary.splice(i, 1);
render();
alert(`You've deleted ${deletedTitle} from your library.`);
});
}
}
36 changes: 31 additions & 5 deletions debugging/book-library/style.css
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
.collapse.show{
display: flex;
}

.form-group {
width: 400px;
height: 300px;
align-self: left;
align-self: flex-start;
padding-left: 20px;
}

.btn {
display: block;
.table .btn,
.form-group .btn {
color: white;
}

.form-check-label {
padding-left: 20px;
margin: 5px 0px 5px 0px;
padding-top: 10px;
margin: 5px ;
}

button.btn-info {
button[data-toggle="collapse"] {
margin: 20px;
}

#add-book-btn {
display: block;
margin-top: 10px;
}

.btn-primary {
background-color: blue;
border-color: blue;
}

.btn-success {
background-color: green;
border-color: green;
}

.btn-danger {
background-color: rgb(199, 0, 0);
border-color: rgb(199, 0, 0);
}
Loading