Skip to content
Open
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
56 changes: 22 additions & 34 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>My 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 @@ -27,23 +24,27 @@ <h1>Library</h1>
Add new book
</button>

<div id="message" class="alert" hidden></div>

<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
minlength="1"
/>
<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
required
minlength="1"
/>
<label for="pages">Pages:</label>
<input
Expand All @@ -52,24 +53,19 @@ <h1>Library</h1>
id="pages"
name="pages"
required
min="1"
step="1"
/>
Comment on lines 50 to 58

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Even with these constraints, currently the browser would still allow a user to submit value like 12.345 or -100. Could you find out why and then fix the issue?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I tested this again and found that decimal and negative page values are now rejected and the error message is displayed. It is possible that this issue occurred before I added the error message.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The error message you mentioned is generated by your JS code, and not by the browser.
The browser will only check the input against those constraints when a user submits a form. Currently those input elements are not inside a form, and that's why the browser does not enforce the constraints.

<label class="form-check-label">
<input
type="checkbox"
class="form-check-input"
id="check"
value=""
/>Read
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="check" />
<label class="form-check-label" for="check"> Read </label>
</div>

<button type="button" id="submitBtn" class="btn btn-primary">
Submit
</button>
</div>
</div>

<table class="table" id="display">
<thead class="thead-dark">
<tr>
Expand All @@ -80,17 +76,9 @@ <h1>Library</h1>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
<tbody id="bookList"></tbody>
</table>

<script src="script.js"></script>
<script src="script.js" type="module"></script>
</body>
</html>
181 changes: 103 additions & 78 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
let myLibrary = [];
const myLibrary = [];

let titleInput;
let authorInput;
let pagesInput;
let checkInput;
let submitBtn;
let bookList;
let messageEl;

function init() {
// Get elements from HTML
titleInput = document.getElementById("title");
authorInput = document.getElementById("author");
pagesInput = document.getElementById("pages");
checkInput = document.getElementById("check");
submitBtn = document.getElementById("submitBtn");
bookList = document.getElementById("bookList");
messageEl = document.getElementById("message");

// Listen for Submit button click
submitBtn.addEventListener("click", submitBook);

// Add initial books
const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
const book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
127,
true
);

myLibrary.push(book1);
myLibrary.push(book2);

window.addEventListener("load", function (e) {
populateStorage();
render();
});

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 title = document.getElementById("title");
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 ||
title.value == "" ||
pages.value == null ||
pages.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();
init();

// Check the right input from forms and if it's okay,
// add the new book object to the array and render
function submitBook() {
const title = titleInput.value.trim();
const author = authorInput.value.trim();
const pages = Number(pagesInput.value);

if (!title || !author || !Number.isInteger(pages) || pages < 1) {
showMessage("Please enter valid book information.", "danger");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When I clicked the Submit button without any input, I couldn't see any error message.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I found the issue: I had not included an element with id="message" in my HTML. I added the missing element, and the validation error message now displays correctly when the Submit button is clicked without any input.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This was the bug I wanted you to find. You fixed it!

return;
}

const book = new Book(title, author, pages, checkInput.checked);

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

// Book constructor
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
Expand All @@ -51,53 +65,64 @@ 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
let length = myLibrary.length;
// Delete old table rows
bookList.replaceChildren();

// Insert updated rows and cells
const length = myLibrary.length;

for (let i = 0; i < length; i++) {
let row = table.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);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;

changeBut.addEventListener("click", function () {
const row = bookList.insertRow();

const titleCell = row.insertCell(0);
const authorCell = row.insertCell(1);
const pagesCell = row.insertCell(2);
const wasReadCell = row.insertCell(3);
const deleteCell = row.insertCell(4);

titleCell.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = myLibrary[i].pages;

// Add and wait for action for read/unread button
const changeButton = document.createElement("button");
changeButton.className = "btn btn-success";

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

wasReadCell.appendChild(changeButton);

changeButton.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}`);
// Add delete button to every row
const deleteButton = document.createElement("button");
deleteButton.className = "btn btn-warning";
deleteButton.textContent = "Delete";

deleteCell.appendChild(deleteButton);

deleteButton.addEventListener("click", function () {
const deletedTitle = myLibrary[i].title;

myLibrary.splice(i, 1);
render();

showMessage(`You've deleted title: ${deletedTitle}`, "success");
});
}
}

// Display a message without using alert()
function showMessage(message, type) {
messageEl.textContent = message;
messageEl.className = `alert alert-${type}`;
messageEl.hidden = false;

setTimeout(function () {
messageEl.hidden = true;
}, 3000);
}
6 changes: 3 additions & 3 deletions debugging/book-library/style.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.form-group {
width: 400px;
height: 300px;
align-self: left;
align-self: flex-start;
padding-left: 20px;
}

Expand All @@ -10,8 +10,8 @@
}

.form-check-label {
padding-left: 20px;
margin: 5px 0px 5px 0px;
padding-left: 5px;
margin: 0px 0px 10px 0px;
}

button.btn-info {
Expand Down
Loading