Skip to content
Closed
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
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
Comment on lines +2 to +4
Copy link
Contributor

Choose a reason for hiding this comment

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

  • JSON != JS. A JSON string containing JS-style comments is considered invalid.

  • Better to use .gitignore to exclude non-essential configuration files from being added to a repo.

Copy link
Author

Choose a reason for hiding this comment

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

  • JSON != JS. A JSON string containing JS-style comments is considered invalid.

    • Better to use .gitignore to exclude non-essential configuration files from being added to a repo.

Thank you, I will see what it really is. It happened accidentally.

"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
2 changes: 1 addition & 1 deletion debugging/book-library/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ <h1>Library</h1>
</tbody>
</table>

<script src="script.js"></script>
<script type="module" src="script.js"></script>
</body>
</html>
16 changes: 12 additions & 4 deletions debugging/book-library/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ My website should be able to:

## Bugs to be fixed

1. Website loads but doesn't show any books
2. Error in console when you try to add a book
3. It uses the title name as the author name
4. Delete button is broken
1. Website loads but doesn't show any books
|Ans: fixed to show books
2. Error in console when you try to add a book
Ans: We can add now.
3. It uses the title name as the author name
Ans: has been fixed
4. Delete button is broken
ANs: has been functional
5. When I add a book that I say I've read - it saves the wrong answer
Ans: it is working normal

I think there are other some other small bugs in my code...but I'm lazy so I can't fix them all.
-- Page number used to accept 0 or even negative numbers. Now it only accept >=1

I wish somebody would help me!

I think I have tried something.
74 changes: 57 additions & 17 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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("Robinson Crusoe", "Daniel Defoe", "252", true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
Expand All @@ -28,21 +28,56 @@ 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() {
// Letters + spaces only (authors)
const lettersOnly = /^[A-Za-z\s]+$/;

// Title: letters + numbers + spaces (no special characters)
const titleAllowed = /^[A-Za-z0-9\s]+$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

These two rules could exclude quite some real-world books.

Copy link
Author

Choose a reason for hiding this comment

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

These two rules could exclude quite some real-world books.

Thank you, It has now been updated to handle wide range of author and title inputs normally.


// Empty field check
if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
title.value.trim() === "" ||
author.value.trim() === "" ||
pages.value.trim() === ""
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
render();
}

// Title validation
if (!titleAllowed.test(title.value)) {
alert("Title must contain only letters, numbers, and spaces!");
return false;
}

// Author validation
if (!lettersOnly.test(author.value)) {
alert("Author name must contain only letters!");
return false;
}

// Pages >= 1
let pagesNum = Number(pages.value);

if (isNaN(pagesNum) || pagesNum < 1) {
alert("Pages must be a number greater than or equal to 1!");
return false;
}

// Create and save the book
let book = new Book(title.value, author.value, pages.value, check.checked);
Copy link
Contributor

Choose a reason for hiding this comment

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

These raw values are not the values you validated. Consider
Step 1: Store preprocessed raw input in some variables
Step 2: Validate the values in the variables
Step 3: Refer to the values of the variables (to avoid accidently using raw input)

Also, some invalid "number of pages" can still pass the current validation rule.

Copy link
Author

Choose a reason for hiding this comment

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

These raw values are not the values you validated. Consider Step 1: Store preprocessed raw input in some variables Step 2: Validate the values in the variables Step 3: Refer to the values of the variables (to avoid accidently using raw input)

Also, some invalid "number of pages" can still pass the current validation rule.

Thank you @cjyuan, the code has been updated to preprocess and validate all input values before storing.

myLibrary.push(book);

// Clear inputs (optional)
title.value = "";
author.value = "";
pages.value = "";
check.checked = false;

render();
}


function Book(title, author, pages, check) {
this.title = title;
this.author = author;
Expand All @@ -53,8 +88,8 @@ 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-- {
// delete old table rows (keep header row 0)
for (let n = rowsNumber - 1; n > 0; n--) {
table.deleteRow(n);
}
//insert updated row and cells
Expand All @@ -72,11 +107,12 @@ function render() {

//add and wait for action for read/unread button
let changeBut = document.createElement("button");
changeBut.id = i;
changeBut.id = "read-btn-" + i;
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
// show "Yes" when check is true (read), otherwise "No"
if (myLibrary[i].check === true) {
readStatus = "Yes";
} else {
readStatus = "No";
Expand All @@ -89,15 +125,19 @@ function render() {
});

//add delete button to every row and render again
let delButton = document.createElement("button");
delBut.id = i + 5;
deleteCell.appendChild(delBut);
let delBut = document.createElement("button");
delBut.id = "del-btn-" + i;
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("clicks", function () {
deleteCell.appendChild(delBut);

delBut.addEventListener("click", function () {
alert(`You've deleted title: ${myLibrary[i].title}`);
myLibrary.splice(i, 1);
render();
});
}
}


// Book Library bugs fixed and Book Library working normal.