Skip to content
Closed
Changes from 1 commit
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
76 changes: 38 additions & 38 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,17 +29,19 @@ 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();
const cleanPages = parseInt(pages.value, 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;
} else if (isNaN(cleanPages) || cleanPages <= 0) { // the value of type of pages must be a integer
alert("Pages must be a positive whole number.");
return false;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

When an if block contains a return statement, we could choose not to use else.

let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
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();
}
}
Expand All @@ -52,11 +55,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 +68,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