Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
127 changes: 64 additions & 63 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,96 +1,97 @@
<!DOCTYPE html>
<!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>
<title>Book Library</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<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" />
<link rel="stylesheet" href="style.css" />
</head>

<body>
<div class="jumbotron text-center">
<header class="header">
<h1>Library</h1>
<p>Add books to your virtual library</p>
</div>

<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
Add new book
</button>
<button
id="add-btn"
data-toggle="collapse"
data-target="#demo"
class="btn btn-primary"
>
Add new book
</button>
</header>

<!-- FORM -->
<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">
<form id="book-form">
<div 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="checkbox"
class="form-check-input"
id="check"
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
</div>
type="number"
class="form-control"
id="pages"
name="pages"
required
/>

<label class="form-check-label">
<input type="checkbox" class="form-check-input" id="check" />
Read
</label>

<button id="submit-btn" class="btn btn-success" type="submit">
Submit
</button>
</div>
</form>
</div>

<!-- TABLE -->
<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>
<th>Actions</th>
</tr>
</thead>

<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<!-- JS will insert rows here -->
</tbody>
</table>

<!-- SCRIPTS -->
<script src="script.js"></script>

<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>
</body>
</html>
135 changes: 77 additions & 58 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,122 @@
let myLibrary = [];
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.

Safer to declare it using const.


window.addEventListener("load", function (e) {
// =====> Load initial data
window.addEventListener("load", function () {
populateStorage();
render();
});

// =====> Default books
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);
myLibrary.push(book2);
render();

myLibrary.push(book1, book2);
}
}

// =====> DOM elements
const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
const bookForm = document.getElementById("book-form");
Comment on lines +24 to +29
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 17, 2026

Choose a reason for hiding this comment

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

  • Could consider declaring these at the beginning of this file to keep all shared variables in one place.

  • Why not rename all these constants to better reflect they are DOM elements? (bookForm is a good name)


// =====> Form submit
bookForm.addEventListener("submit", function (e) {
e.preventDefault();
submit();
});
Comment on lines +31 to +35
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.

Better to perform this in the window's on load event listener so that all code that needs to be executed once on page load in located in one place.


//check the right input from forms and if its ok -> add the new book (object in array)
//via Book function and start render function
// =====> Add new book
function submit() {
if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
) {
const titleValue = title.value.trim();
const authorValue = author.value.trim();
const pagesValue = pages.value.trim();

// Validation (improved)
if (!titleValue || !authorValue || !pagesValue) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
return;
}

// Create new book
let book = new Book(
titleValue,
authorValue,
Number(pagesValue),
check.checked
);

myLibrary.push(book);

render();
bookForm.reset();

// safely close bootstrap form (optional chaining operator)
document.getElementById("demo")?.classList.remove("show");
}

// =====> Book constructor
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
}

// =====> Render table
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);

// clear old rows (keep header)
for (let i = table.rows.length - 1; i > 0; i--) {
table.deleteRow(i);
}
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {

// render books
myLibrary.forEach((book, index) => {
let row = table.insertRow(1);

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;

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
let readCell = row.insertCell(3);
let actionCell = row.insertCell(4);

titleCell.textContent = book.title;
authorCell.textContent = book.author;
pagesCell.textContent = book.pages;

// =====> Read button
let readBtn = document.createElement("button");
readBtn.className = "btn btn-success";
readBtn.textContent = book.check ? "Read" : "Unread";

readBtn.addEventListener("click", function () {
book.check = !book.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}`);
myLibrary.splice(i, 1);
readCell.appendChild(readBtn);

// =====> Delete button
let deleteBtn = document.createElement("button");
deleteBtn.className = "btn btn-warning";
deleteBtn.textContent = "Delete";

deleteBtn.addEventListener("click", function () {
alert(`Deleted: ${book.title}`);
myLibrary.splice(index, 1);
render();
Comment on lines +115 to 117
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.

The alert message is shown before the book is actually deleted; the deletion only occurs after the alert dialog is dismissed. This introduces a risk that the operation may not complete (e.g., if the user closes the browser before dismissing the alert).

In general, it’s better to display a confirmation message only after the associated operation has successfully completed.

});
}

actionCell.appendChild(deleteBtn);
});
}
Loading
Loading