Skip to content
Closed
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
17 changes: 5 additions & 12 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<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 @@ -31,19 +28,17 @@ <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"
required
/>
<label for="pages">Pages:</label>
<input
Expand Down Expand Up @@ -80,15 +75,13 @@ <h1>Library</h1>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

<script src="script.js"></script>
Expand Down
88 changes: 47 additions & 41 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


let myLibrary = [];

window.addEventListener("load", function (e) {
Expand All @@ -7,16 +9,15 @@ window.addEventListener("load", function (e) {

function populateStorage() {
if (myLibrary.length == 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
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);
myLibrary.push(book2);
render();
}
}

Expand All @@ -28,19 +29,27 @@ 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!");
const cleanTitle = title.value.trim();
const cleanAuthor = author.value.trim();

if (!/^[1-9]\d*$/.test(pages.value.trim())) { // plain positive whole numbers like 1, 25, 300 — no letters, no decimal points, no scientific notation
alert("Pages must be a positive whole number without letters or symbols.");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}

const cleanPages = parseInt(pages.value.trim(), 10);

if (!cleanTitle|| !cleanAuthor) { // title and author doesn't allowed to contain only space characters
alert("Title and Author cannot be empty or spaces only.");
return false;

} if (isNaN(cleanPages) || cleanPages <= 0) { // the value of type of pages must be a integer
Copy link
Contributor

Choose a reason for hiding this comment

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

Without else, common practice is to start an if block on a separate line.

alert("Pages must be a positive whole number.");
return false;
}
let book = new Book(cleanTitle, cleanAuthor, pages.value, check.checked); //// title and author be allowed to contain leading or trailing space characters, we storage using parseInt
Copy link
Contributor

Choose a reason for hiding this comment

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

pages.value could be "3e1" (which could pass the check on line 39).

Copy link
Author

Choose a reason for hiding this comment

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

@cjyuan
I fixed all the errors

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use cleanPages? pages.value is a string and can still have leading and trailing space characters.

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

function Book(title, author, pages, check) {
Expand All @@ -52,11 +61,10 @@ 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);
}
// Keep only the header row
table.innerHTML = table.rows[0].outerHTML;
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the <table> also contains a <tbody> child element. The statement on line 60 will remove the <tbody> element.

An alternative approach is the clear <tbody> directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

No change?

Copy link
Author

Choose a reason for hiding this comment

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

I removed from index.html


//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
Expand All @@ -66,37 +74,35 @@ function render() {
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;
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 toggleReadButton = document.createElement("button");
toggleReadButton.className = "btn btn-success";
wasReadCell.appendChild(toggleReadButton);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;

changeBut.addEventListener("click", function () {

readStatus = myLibrary[i].check ? "Yes" : "No";

toggleReadButton.innerText = readStatus;

toggleReadButton.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
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}`);
myLibrary.splice(i, 1);
let deleteBookButton = document.createElement("button");
deleteCell.appendChild(deleteBookButton);
deleteBookButton.className = "btn btn-warning";
deleteBookButton.innerHTML = "Delete";
deleteBookButton.addEventListener("click", function () {
const deleteTitle = myLibrary[i].title; //store first
myLibrary.splice(i, 1); // delete it
alert(`You have deleted title: ${deleteTitle}`);
render();
});
}
Expand Down
Loading