Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
eac5914
Fix syntax: missing closing brace
RaihanSharif Mar 6, 2026
d09f10d
Fix: Render all books on page load and when adding new books using form
RaihanSharif Mar 6, 2026
e5c8606
change let to const on initial books to prevent accidental reassignment
RaihanSharif Mar 6, 2026
fcb63be
renamed check to completed for clarity
RaihanSharif Mar 6, 2026
d5aef67
changed to match naming in the javascript, and for clarity.
RaihanSharif Mar 6, 2026
7825b00
Fix: Display correct completed status and change button
RaihanSharif Mar 6, 2026
2ddbdb4
fix delete button
RaihanSharif Mar 6, 2026
4b9e8a1
remove unnecessary id on change button
RaihanSharif Mar 6, 2026
c933c4d
remove unnecessary render in load
RaihanSharif Mar 6, 2026
9e475ec
remove unnecessary id in delete button
RaihanSharif Mar 6, 2026
481c9d2
reset input fields after adding new book
RaihanSharif Mar 6, 2026
92aa7dd
prevent duplicate book entries
RaihanSharif Mar 6, 2026
219e37e
remove dead code
RaihanSharif Mar 6, 2026
e6f827f
rename completed btn for clarity
RaihanSharif May 8, 2026
c1db8fb
change color of completed btn to reflect status
RaihanSharif May 8, 2026
cb062e2
fix input name, id and type values
RaihanSharif May 14, 2026
d1a2249
fix author input type
RaihanSharif May 14, 2026
1e893fa
add script as a module
RaihanSharif May 14, 2026
fc959f4
input validation
RaihanSharif May 14, 2026
08d6c4c
save page count as a number
RaihanSharif May 14, 2026
47c95a9
remove null checks of input fields in submit
RaihanSharif May 14, 2026
a9f157e
trim and validate input
RaihanSharif May 14, 2026
fc74c9b
replace all innerHTML with textContent
RaihanSharif May 14, 2026
766487c
fix render
RaihanSharif May 14, 2026
39a9048
fix meta tag
RaihanSharif May 14, 2026
0f5ee32
update min and max length to be more in line with VARCHAR used in dat…
RaihanSharif May 14, 2026
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
38 changes: 20 additions & 18 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<!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"
/>
<title>Book library app</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"
/>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="module" src="script.js"></script>
</head>

<body>
Expand All @@ -29,20 +27,24 @@ <h1>Library</h1>

<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<label for="book-title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
id="book-title"
name="book-title"
minlength="1"
maxlength="255"
required
/>
<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
minlength="1"
maxlength="255"
required
/>
<label for="pages">Pages:</label>
Expand All @@ -51,6 +53,8 @@ <h1>Library</h1>
class="form-control"
id="pages"
name="pages"
min="1"
max="10000"
required
/>
<label class="form-check-label">
Expand All @@ -59,13 +63,13 @@ <h1>Library</h1>
class="form-check-input"
id="check"
value=""
/>Read
/>completed
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
id="submit-btn"
/>
</div>
</div>
Expand All @@ -76,7 +80,7 @@ <h1>Library</h1>
<th>Title</th>
<th>Author</th>
<th>Number of Pages</th>
<th>Read</th>
<th>Completed</th>
<th></th>
</tr>
</thead>
Expand All @@ -90,7 +94,5 @@ <h1>Library</h1>
</tr>
</tbody>
</table>

<script src="script.js"></script>
</body>
</html>
134 changes: 79 additions & 55 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,125 @@
let myLibrary = [];
const myLibrary = [];

window.addEventListener("load", function (e) {
populateStorage();
render();
});

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
let book2 = new Book(
const book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
const book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
"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("book-title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const isReadCheckbox = document.getElementById("check");
const submitBtn = document.getElementById("submit-btn");

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

if (title === "") {
alert("Please fill title field!");
return false;
}

if (!Number.isInteger(pages) || pages < 1) {
alert("Pages must be a whole number greater than 0!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}

const book = new Book(title, author, pages, isReadCheckbox.checked);

if (isBookInLibrary(book)) {
alert("This book is already in the library!");
return false;
}
myLibrary.push(book);
resetInputFields();
render();
}

submitBtn.addEventListener("click", submit);

function isBookInLibrary(newBook) {
return myLibrary.some(
(old) =>
old.title.toLowerCase() === newBook.title.toLowerCase() &&
old.author.toLowerCase() === newBook.author.toLowerCase()
);
}

function Book(title, author, pages, check) {
function resetInputFields() {
titleInput.value = "";
authorInput.value = "";
pagesInput.value = "";
isReadCheckbox.checked = false;
}

function Book(title, author, pages, completed) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
this.completed = completed;
}

function render() {
let table = document.getElementById("display");
let rowsNumber = table.rows.length;
const table = document.getElementById("display");
const rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- {
for (let n = rowsNumber - 1; n > 0; n--) {
table.deleteRow(n);
}
//insert updated row and cells
let length = myLibrary.length;
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;
const row = table.insertRow(1);
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 = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = 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;
const changeCompletedBtn = document.createElement("button");
changeCompletedBtn.className = "btn";
wasReadCell.appendChild(changeCompletedBtn);
changeCompletedBtn.classList.add(
myLibrary[i].completed ? "btn-success" : "btn-secondary"
);
changeCompletedBtn.textContent = myLibrary[i].completed ? "Yes" : "No";

changeCompletedBtn.addEventListener("click", function () {
myLibrary[i].completed = !myLibrary[i].completed;
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}`);
const delBtn = document.createElement("button");
deleteCell.appendChild(delBtn);
delBtn.className = "btn btn-warning";
delBtn.textContent = "Delete";
delBtn.addEventListener("click", function () {
const deletedBook = myLibrary[i].title;
if (!confirm(`Delete "${deletedBook}?`)) return;
myLibrary.splice(i, 1);
render();
});
Expand Down
Loading