Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
116 changes: 58 additions & 58 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
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", function () {
populateStorage();
render();
});

submitBtn.addEventListener("click", addBook);

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
);
myLibrary.push(book1);
myLibrary.push(book2);
render();
const storedLibrary = localStorage.getItem("myLibrary");
if (storedLibrary) {
myLibrary = JSON.parse(storedLibrary);
}
Comment on lines 19 to 37
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Shouldn't myLibrary be populated with some data for users who do not have any data in their browser's local storage?

  • The objects in the restored array are not instances of Book. A a result, myLibrary will end up with two kinds of objects: Generic objects and Book objects (added on line 42). Even though this is not a problem for this simple app, it is a better practice to consistently keep the same type of data in myLibrary.

Copy link
Contributor

@cjyuan cjyuan Dec 19, 2025

Choose a reason for hiding this comment

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

Can you also address the comment I left on the code on lines 18-21 in the previous review?

Copy link
Author

Choose a reason for hiding this comment

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

To populate data for users who do not have any, and to restore plain data objects back into book objects, the following code snippet has been applied:

  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)
    );
  } 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();
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

Well done.

Please note that if pages are consistently stored values of type number, we would not need the conversion Number(data.pages).

}

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
function saveStorage() {
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
}

// Validates input and adds new book
function addBook(e) {
if (e) e.preventDefault();

//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 == ""
) {
if (!titleInput.value || !authorInput.value || !pagesInput.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();
}
let book = new Book(
titleInput.value,
authorInput.value,
pagesInput.value,
readCheckbox.checked
);
myLibrary.push(book);
saveStorage();
render();
}

function Book(title, author, pages, check) {
Expand All @@ -51,52 +52,51 @@ 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
// Clears table body efficiently
const tbody = table.querySelector("tbody");
tbody.innerHTML = "";

// Inserts updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
// Insert at the end of the table
let row = tbody.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);
// Uses textContent to prevent XSS
titleCell.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = myLibrary[i].pages;

// Toggles read status
let readBtn = document.createElement("button");
wasReadCell.appendChild(readBtn);

let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
if (myLibrary[i].check === false) {
readStatus = "No";
} else {
readStatus = "Yes";
}
changeBut.innerText = readStatus;
readBtn.textContent = readStatus;

changeBut.addEventListener("click", function () {
readBtn.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].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 book
let deleteBtn = document.createElement("button");
deleteCell.appendChild(deleteBtn);
deleteBtn.textContent = "Delete";
deleteBtn.addEventListener("click", function () {
myLibrary.splice(i, 1);
saveStorage();
render();
});
}
Expand Down