Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

London | Nadarajah_Sripathmanathan | book -library | Module-Data-Flows #178

Closed
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
2 changes: 1 addition & 1 deletion debugging/book-library/index.html
Copy link

Choose a reason for hiding this comment

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

When I checked the code in index.html at https://validator.w3.org/, it indicated some errors.

Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ <h1>Library</h1>

<script src="script.js"></script>
</body>
</html>
</html>
24 changes: 9 additions & 15 deletions debugging/book-library/script.js
Copy link

Choose a reason for hiding this comment

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

In terms of input validation,

  1. Are all input properly checked?
  2. Can .value be null? (Do we need to check someInputElement.value == null?)
  3. What if a user enters only space characters in the "title" input field?
  4. What if a users enters -1 or 3.1416 in the "pages" input field?

With the way the book's title is assigned to an HTML element, a book with a title containing special character sequence such as <i> can possibly ruin the display.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ 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 ||
Expand All @@ -37,8 +35,8 @@ function submit() {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
let book = new Book(title.value, author.value, pages.value, check.checked);
myLibrary.push(book);
render();
}
}
Expand All @@ -53,11 +51,9 @@ 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-- {
for (let n = rowsNumber - 1; n > 0; n--) {
table.deleteRow(n);
}
Comment on lines +54 to 56
Copy link

Choose a reason for hiding this comment

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

Instead of deleting the table rows one by one, can you think of a more efficient way to remove all rows (except the <th>...</th>) in the table?

//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
Expand All @@ -70,7 +66,6 @@ function render() {
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";
Expand All @@ -88,16 +83,15 @@ function render() {
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 () {
delButton.id = i + 5;
deleteCell.appendChild(delButton);
delButton.className = "btn btn-warning";
delButton.innerHTML = "Delete";
delButton.addEventListener("click", function () {
Comment on lines +87 to +91
Copy link

Choose a reason for hiding this comment

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

  • Are the value assigned to the id attribute of changeBut and delButton unique?
  • Is there a need to assign an id attribute to delButton?
  • Is there a need to assign an id attribute to changeBut?
  • Can you think of a more consistent way to name the variables representing the two buttons, changeBut and delButton?

alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
Comment on lines 92 to 93
Copy link

Choose a reason for hiding this comment

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

At the moment when the alert message is displayed, has the book mentioned in the message been deleted yet?
How would you rearrange these statements to make the message truthful?

render();
});
}
}
}