Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
83 changes: 47 additions & 36 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<!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"
/>
<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>
<meta charset="utf-8" />
<title>Library — Add books to your virtual library</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- Styles -->
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
Expand All @@ -23,50 +19,60 @@ <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 +83,25 @@ <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>
<!-- Designated notice container controlled via style.css -->
<div
id="alerts"
class="notice-root"
role="status"
aria-live="polite"
aria-atomic="true"
></div>

<!-- Scripts loaded at end of body -->
<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>
<script src="script.js" type="module"></script>
</body>
</html>
166 changes: 105 additions & 61 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,93 @@ 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);
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();
}
}

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 using CSS animation; JS only toggles classes
function showSuccess(message) {
const root = document.getElementById("alerts");
if (!root) return;

const alertEl = document.createElement("div");
// Use Bootstrap alert styling + our notice classes
alertEl.className = "alert alert-success notice notice--auto-hide";
alertEl.setAttribute("role", "status");
alertEl.textContent = message;

// Remove the element after the CSS animation completes (no setTimeout)
alertEl.addEventListener(
"animationend",
() => {
alertEl.remove();
},
{ once: true }
);

root.appendChild(alertEl);
}

//check the right input from forms and if its ok -> add the new book (object in array)
//via Book function and start render function
// Check the right input from forms and if it's 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();

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

// Read and validate pages as a positive integer
const pages = pagesInput.valueAsNumber;
const v = pagesInput.validity;

if (v.valueMissing || !Number.isFinite(pages)) {
alert("Pages is required and must be a number.");
return;
}
if (v.rangeUnderflow || pages < 1) {
alert("Pages must be at least 1.");
return;
}
// Enforce whole numbers only (reject decimals like 3.5)
if (v.stepMismatch || !Number.isInteger(pages)) {
alert("Pages must be a whole 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 +100,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 = String(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);
}
}
44 changes: 44 additions & 0 deletions debugging/book-library/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,47 @@
button.btn-info {
margin: 20px;
}

/* --- Notices (toast) styles --- */

/* Container to control placement of notices */
.notice-root {
position: fixed;
top: 1rem;
right: 1rem;
z-index: 1050;
display: flex;
flex-direction: column;
gap: 0.5rem;
pointer-events: none; /* allow clicks to pass through container */
}

/* Each notice uses Bootstrap alert styles, plus these */
.notice {
pointer-events: auto; /* allow focus/click on the notice itself if needed */
}

/* Pure-CSS lifecycle: fade in, hold, fade out */
@keyframes notice-life {
0% {
opacity: 0;
transform: translateY(-6px);
}
10% {
opacity: 1;
transform: translateY(0);
}
85% {
opacity: 1;
transform: translateY(0);
}
100% {
opacity: 0;
transform: translateY(-6px);
}
}

/* Apply the animation */
.notice--auto-hide {
animation: notice-life 2500ms ease-in-out forwards;
}