Skip to content
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e2bf444
Fix syntax error in for-loop (line 57) that blocked book list from re…
Iswanna Dec 9, 2025
c39e375
Fix: rename delButton to delBut and correct event to 'click' to enabl…
Iswanna Dec 9, 2025
c794174
Rename library to myLibrary in submit() so new books are added to the…
Iswanna Dec 9, 2025
436136b
fix: use author.value for author field when creating a new Book (line…
Iswanna Dec 9, 2025
dccb8a5
fix(render): correct read status display so checked=true shows “Yes” …
Iswanna Dec 9, 2025
07c6620
Remove redundant render() call in populateStorage (line 19) to avoid …
Iswanna Dec 9, 2025
085a373
Remove placeholder table row <tr> from <tbody> to allow render() to m…
Iswanna Dec 10, 2025
4bf7b31
Fix invalid HTML input types by changing <input type="title"/> and <i…
Iswanna Dec 10, 2025
d9870cf
Switch script.js to type="module", remove inline onclick="submit()", …
Iswanna Dec 10, 2025
3acd315
Add id="submitBtn" to the submit button to enable attaching an event …
Iswanna Dec 10, 2025
dae6c85
Refactor code to remove redundant null checks on input values, ensure…
Iswanna Dec 10, 2025
a6619ba
Refactor variable names for clarity: DOM inputs now use descriptive n…
Iswanna Dec 10, 2025
d413566
fix(js): add full input validation for author and page count
Iswanna Dec 10, 2025
c784153
refactor(js): optimize table clearing using tbody.innerHTML
Iswanna Dec 10, 2025
4757d2f
Clear rows via tbody.innerHTML to preserve header and speed up re-render
Iswanna Dec 10, 2025
0a4c4ae
ux(delete): confirm before deletion and show non-blocking success alert
Iswanna Dec 10, 2025
757fcd3
Correct book title spelling from “Robison Crusoe” to “Robinson Crusoe…
Iswanna Dec 10, 2025
2b98534
Fix(form): prevent default submit to avoid page reload on Submit butt…
Iswanna Dec 10, 2025
63652a8
Chore(html): add lang, split meta tags, set meaningful title, and mak…
Iswanna Dec 10, 2025
7c28cc2
Add #alerts container and move the dependency scripts to end of the body
Iswanna Dec 11, 2025
9526485
Fix indentation inconsistency in index.html
Iswanna Dec 11, 2025
9c3c39f
refactor(notices): use #alerts container with CSS-only auto-hide; rem…
Iswanna Dec 11, 2025
2417ca4
fix(validation): require pages to be a positive integer
Iswanna Dec 11, 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
71 changes: 37 additions & 34 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<!DOCTYPE html>
<html>
<!doctype html>
<html lang="en">
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<meta charset="utf-8">
<title>Library — Add books to your virtual library</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Dependencies -->
<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>

<!-- Styles -->
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
Expand All @@ -23,50 +24,59 @@ <h1>Library</h1>
<p>Add books to your virtual library</p>
</div>

<button data-toggle="collapse" data-target="#demo" class="btn btn-info">
<button data-toggle="collapse" data-target="#demo" class="btn btn-info" type="button">
Add new book
</button>

<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
/>
<label for="author">Author: </label>

<label for="author">Author:</label>
<input
type="author"
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"
step="1"
required
/>
<label class="form-check-label">

<div class="form-check mt-2">
<input
type="checkbox"
class="form-check-input"
id="check"
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
name="check"
/>
<label class="form-check-label" for="check">Read</label>
</div>

<!-- Explicit type="button" to avoid default form submission -->
<button
type="button"
class="btn btn-primary mt-3"
id="submitBtn"
>
Submit
</button>
</div>
</div>

Expand All @@ -77,20 +87,13 @@ <h1>Library</h1>
<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>
</tbody>
<tbody></tbody>
</table>

<script src="script.js"></script>
<!-- Load script at end of body; modules are deferred automatically -->
<script src="script.js" type="module"></script>
</body>
</html>
</html>
144 changes: 86 additions & 58 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,77 @@ let myLibrary = [];
window.addEventListener("load", function (e) {
populateStorage();
render();

// Attach submit listener (prevent default form submission)
const submitBtn = document.getElementById("submitBtn");
if (submitBtn) {
submitBtn.addEventListener("click", function (evt) {
evt.preventDefault();
submit();
});
}
});

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
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();
}
}

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const readCheckbox = document.getElementById("check");

// Non-blocking success message (appears briefly and fades out)
function showSuccess(message) {
const alertEl = document.createElement("div");
alertEl.className = "alert alert-success";
alertEl.textContent = message;
alertEl.style.position = "fixed";
alertEl.style.top = "1rem";
alertEl.style.right = "1rem";
alertEl.style.zIndex = "1050";
document.body.appendChild(alertEl);

setTimeout(() => {
alertEl.style.transition = "opacity 0.3s";
alertEl.style.opacity = "0";
setTimeout(() => alertEl.remove(), 300);
}, 2500);
}
Comment on lines 37 to 57
Copy link
Contributor

Choose a reason for hiding this comment

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

(Optional)
Could consider designating an element in index.html to display the message. This way, we can have more control where to position the element.

Separation of concern -- We should avoid specifying CSS styles in JS or in HTML file. (Assigning CSS class is ok)

It is possible to create the effect of "making the foreground visible for a fixed amount of time" using only CSS. And then in JS we can just add/remove CSS class to trigger the animation without using setTimeout().

Copy link
Author

@Iswanna Iswanna Dec 11, 2025

Choose a reason for hiding this comment

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

Thank you for the suggestion. I’ve made the updates to the code based on your feedback.


//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();
const title = titleInput.value.trim();
const author = authorInput.value.trim();
const pages = Number(pagesInput.value);

// Validate title and author
if (title === "" || author === "") {
alert("Title and author cannot be empty.");
return;
}

// Validate pages
if (Number.isNaN(pages) || pages <= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Some invalid "number of pages" can still pass this check.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback. I have updated the code to fix this bug.

alert("Pages must be a positive number.");
return;
}

// Create and add book
let book = new Book(title, author, pages, readCheckbox.checked);
myLibrary.push(book);
render();
}

function Book(title, author, pages, check) {
Expand All @@ -51,53 +84,48 @@ 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
let length = myLibrary.length;
const table = document.getElementById("display");
const tbody = table.tBodies[0] || table.createTBody();

// Clear all data rows while preserving the header
tbody.innerHTML = "";

const length = myLibrary.length;
for (let i = 0; i < length; i++) {
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;
const book = myLibrary[i];
const row = tbody.insertRow();

const titleCell = row.insertCell(0);
const authorCell = row.insertCell(1);
const pagesCell = row.insertCell(2);
const wasReadCell = row.insertCell(3);
const deleteCell = row.insertCell(4);

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

// Read/unread toggle button
const changeBut = document.createElement("button");
changeBut.className = "btn btn-success";
changeBut.textContent = book.check ? "Yes" : "No";
changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});
wasReadCell.appendChild(changeBut);

//add delete button to every row and render again
let delButton = document.createElement("button");
delBut.id = i + 5;
deleteCell.appendChild(delBut);
// Delete button (confirm before deleting, then show non-blocking success)
const delBut = document.createElement("button");
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
delBut.textContent = "Delete";
delBut.addEventListener("click", function () {
if (confirm(`Are you sure you want to delete "${book.title}"?`)) {
myLibrary.splice(i, 1);
render();
showSuccess(`Deleted "${book.title}"`);
}
});
deleteCell.appendChild(delBut);
}
}