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
55 changes: 23 additions & 32 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Book Library</title>
<meta charset="utf-8" />
<meta 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>
Expand All @@ -19,7 +16,7 @@

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

Expand All @@ -31,15 +28,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 @@ -64,32 +61,26 @@ <h1>Library</h1>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
class="btn btn-primary btn-block"
id="submit-book-btn"
/>
</div>
</div>

<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>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<div class="table-responsive">
<table class="table table-hover" id="display">
<thead class="thead-dark">
<tr>
<th>Title</th>
<th>Author</th>
<th>Number of Pages</th>
<th>Read</th>
<th></th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>

<script src="script.js"></script>
</body>
Expand Down
169 changes: 95 additions & 74 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const readCheckbox = document.getElementById("check");
const table = document.getElementById("display");
const submitBtn = document.getElementById("submit-book-btn");

let myLibrary = [];

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

submitBtn.addEventListener("click", addBook);

// Initialises data and seeds default books if storage is empty
function populateStorage() {
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",
true
const storedLibrary = localStorage.getItem("myLibrary");

if (storedLibrary) {
const rawData = JSON.parse(storedLibrary);
// Rehydrates plain data back into Book objects
myLibrary = rawData.map(
(data) =>
new Book(data.title, data.author, Number(data.pages), data.check)
);
myLibrary.push(book1);
myLibrary.push(book2);
render();
} else {
// Seeds data for new users
myLibrary = [
new Book("The Hobbit", "J.R.R. Tolkien", 295, false),
new Book("1984", "George Orwell", 328, true),
new Book("Robinson Crusoe", "Daniel Defoe", 252, true),
new Book("The Old Man and the Sea", "Ernest Hemingway", 127, false),
];
saveStorage();
}
}

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
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();
function saveStorage() {
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
}

// Trims input values and prevents empty submissions
function addBook(e) {
if (e) e.preventDefault();

// Trims input whitespace to sanitise entries
const title = titleInput.value.trim();
const author = authorInput.value.trim();
const pages = Number(pagesInput.value);

// Validates input: checks for empty strings, non-numbers, or negative values
if (!title || !author || isNaN(pages) || pages <= 0) {
alert("Please enter valid book details. Pages must be a positive number.");
return;
}
const book = new Book(title, author, pages, readCheckbox.checked);
Copy link
Contributor

Choose a reason for hiding this comment

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

On line 41, the value of pages can still be a string that represents an invalid number of pages.
Can you ensure the value of pages represents a valid number of pages.

Copy link
Author

Choose a reason for hiding this comment

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

To address this I've made the following changes:

  const pages = Number(pagesInput.value);

  // Validates input: checks for empty strings, non-numbers, or negative values
  if (!title || !author || isNaN(pages) || pages <= 0) {
    alert("Please enter valid book details. Pages must be a positive number.");
    return;
  }
  const book = new Book(title, author, pages, readCheckbox.checked);

Copy link
Contributor

Choose a reason for hiding this comment

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

Not all positive finite numbers are valid "number of pages" though. What other numbers should also be rejected?

Copy link
Author

@Tarawally Tarawally Dec 21, 2025

Choose a reason for hiding this comment

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

float would be a problem so i guess this would be better

if (!title || !author || isNaN(pages) || pages <= 0 || !Number.isInteger(pages)) {}

Copy link
Author

Choose a reason for hiding this comment

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

this could also be shorten to

if (!title || !author || !Number.isInteger(pages) || pages <= 0) {}

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

// Resets the entry form after adding a book
titleInput.value = "";
authorInput.value = "";
pagesInput.value = "";
readCheckbox.checked = false;
}

function Book(title, author, pages, check) {
Expand All @@ -51,53 +75,50 @@ 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;
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;

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
const tbody = table.querySelector("tbody");
tbody.innerHTML = "";

myLibrary.forEach((book, i) => {
const row = tbody.insertRow(-1);

// Inserts new cells into the table row
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);

// Prevents XSS by inserting data as textContent
titleCell.textContent = book.title;
authorCell.textContent = book.author;
pagesCell.textContent = book.pages;

// Toggles the read status with a ternary operator
const readBtn = document.createElement("button");
readBtn.className = book.check
? "btn btn-success"
: "btn btn-outline-secondary";
readBtn.textContent = book.check ? "Yes" : "No";
wasReadCell.appendChild(readBtn);

readBtn.addEventListener("click", () => {
book.check = !book.check;
saveStorage();
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}`);
// Deletes the book and notifies the user
const deleteBtn = document.createElement("button");
deleteBtn.className = "btn btn-danger btn-sm";
deleteBtn.textContent = "Delete";
deleteCell.appendChild(deleteBtn);

deleteBtn.addEventListener("click", () => {
const deletedTitle = book.title;
myLibrary.splice(i, 1);
saveStorage();
render();
alert(`Success: "${deletedTitle}" has been removed.`);
});
}
});
}
Loading