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
94fbdbb
Installed the required packages.
anosidium Dec 12, 2025
2bf1010
Add a missing closing bracket in the for loop
anosidium Dec 12, 2025
e700fa3
Rename delBut references to delButton
anosidium Dec 12, 2025
4655a25
Rename library reference to myLibrary
anosidium Dec 12, 2025
53ddf7a
Improve submit() function by setting the book author as author rather…
anosidium Dec 12, 2025
8a518a2
Fix a bug in render() function where setting the book read with the o…
anosidium Dec 12, 2025
6c5e084
Fix the delete button event type and format the code to make it readable
anosidium Dec 12, 2025
0c6f33a
Remove render() function call in load because populateStorage() calls…
anosidium Dec 12, 2025
ad0d3d8
Replace innerHTML with textContent
anosidium Dec 12, 2025
0150ccb
Set the webpage title
anosidium Dec 12, 2025
9266f43
Fix inputs type
anosidium Dec 12, 2025
19e7a6a
Replace "onclick" attribute with "id" and use JavaScript to handle it
anosidium Dec 12, 2025
4a702c6
Fix HTML errors
anosidium Dec 13, 2025
c57ff1a
Load script as an ES module
anosidium Dec 13, 2025
4664133
Format code to be more self-descriptive
anosidium Dec 13, 2025
949633d
Fix the CSS error
anosidium Dec 13, 2025
6b6eaf6
Improve form validation
anosidium Dec 13, 2025
b04676f
Display deletion alert after deleting the book from the library
anosidium Dec 13, 2025
df7d65f
Remove id attributes
anosidium Dec 13, 2025
e2fcbf3
Rename checked attribute to isRead
anosidium Dec 13, 2025
75b81e7
Fix a bug reading the checkbox and simplify setting the read button s…
anosidium Dec 14, 2025
ecba7d7
Improve pages validation
anosidium Dec 14, 2025
9dd47d3
Consistently store pages as a number
anosidium Dec 14, 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
30 changes: 10 additions & 20 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>My Book Library</title>
<meta charset="utf-8" />
<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>
Expand All @@ -31,15 +27,15 @@ <h1>Library</h1>
<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>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
Expand All @@ -51,6 +47,8 @@ <h1>Library</h1>
class="form-control"
id="pages"
name="pages"
min="1"
step="1"
required
/>
<label class="form-check-label">
Expand All @@ -65,7 +63,7 @@ <h1>Library</h1>
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
id="submit"
/>
</div>
</div>
Expand All @@ -80,17 +78,9 @@ <h1>Library</h1>
<th></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>
<script src="script.js" type="module"></script>
</body>
</html>
120 changes: 63 additions & 57 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
let myLibrary = [];
const myLibrary = [];

window.addEventListener("load", function (e) {
populateStorage();
render();
document.getElementById("submit").addEventListener("click", 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("Robison 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,84 +20,90 @@ 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
// 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 titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const isReadInput = document.getElementById("check");

const titleValue = titleInput.value.trim();
const authorValue = authorInput.value.trim();
const pagesRaw = pagesInput.value.trim();
const pagesValue = Number(pagesRaw);

if (titleValue.length === 0 || authorValue.length === 0) {
alert("Title or author cannot be blank!");
return false;
} else if (!Number.isInteger(pagesValue) || pagesRaw < 1) {
alert("Pages must be a positive whole number!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
let book = new Book(
titleValue,
authorValue,
pagesValue,
isReadInput.checked
);

myLibrary.push(book);
render();
}
}

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

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 tableBody = table.querySelector("tbody");

// Delete old table
if (!tableBody) return;
tableBody.innerHTML = "";

// Insert updated row and cells
let length = myLibrary.length;

for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = tableBody.insertRow(0);
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;

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 changeButton = document.createElement("button");
changeButton.className = "btn btn-success";
wasReadCell.appendChild(changeButton);
changeButton.innerText = myLibrary[i].isRead ? "Yes" : "No";

changeButton.addEventListener("click", function () {
myLibrary[i].isRead = !myLibrary[i].isRead;
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}`);
// Add delete button to every row and render again
let deleteButton = document.createElement("button");
deleteCell.appendChild(deleteButton);
deleteButton.className = "btn btn-warning";
deleteButton.textContent = "Delete";

deleteButton.addEventListener("click", function () {
const bookTitle = myLibrary[i].title;
myLibrary.splice(i, 1);
render();
alert(`You've deleted title: ${bookTitle}`);
});
}
}
2 changes: 1 addition & 1 deletion debugging/book-library/style.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.form-group {
width: 400px;
height: 300px;
align-self: left;
align-self: flex-start;
padding-left: 20px;
}

Expand Down
Loading