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
41 changes: 10 additions & 31 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<!DOCTYPE html>
<html>
<!doctype html>
<html lang="en">
<head>
<title> </title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Book Library</title>
<meta charset="utf-8" />
<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 @@ -30,42 +26,25 @@ <h1>Library</h1>
<div id="demo" class="collapse">
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
class="form-control"
id="title"
name="title"
required
/>
<input type="text" class="form-control" id="title"/>
<label for="author">Author: </label>
<input
type="author"
class="form-control"
id="author"
name="author"
required
/>
<input type="text" class="form-control" id="author" name="author"/>
<label for="pages">Pages:</label>
<input
type="number"
class="form-control"
id="pages"
name="pages"
required
/>
<input type="number" class="form-control" id="pages" name="pages" />
<label class="form-check-label">
<input
type="checkbox"
class="form-check-input"
id="check"
value=""
required
/>Read
</label>
<input
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
id="submit"
/>
</div>
</div>
Expand All @@ -91,6 +70,6 @@ <h1>Library</h1>
</tbody>
</table>

<script src="script.js"></script>
<script src="script.js" defer></script>
</body>
</html>
90 changes: 47 additions & 43 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
let myLibrary = [];
const myLibrary = [];

const titleInput = document.getElementById("title");
const authorElement = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const checkOption = document.getElementById("check");

window.addEventListener("load", function (e) {
populateStorage();
render();
const submitInput = document.getElementById("submit");
submitInput.addEventListener("click", submit);
});

function populateStorage() {
Expand All @@ -20,41 +26,43 @@ function populateStorage() {
}
}

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();
const title = titleInput.value.trim();
const author = authorElement.value.trim();
const pages = Number(pagesInput.value.trim());
const isChecked = checkOption.checked;

if (!title || !author || Number.isNaN(pages) || pages <= 0) {
alert(
"Please fill all fields and make sure book page is greater than zero!"
);
Comment thread
cjyuan marked this conversation as resolved.
return;
}

if (!/^[A-Za-z\s]+$/.test(author) || !/^[A-Za-z\s]+$/.test(title)) {
alert("Author and book title must contain only letters");
return;
}

let book = new Book(title, author, pages, isChecked);
myLibrary.push(book);
render();
}

function Book(title, author, pages, check) {
function Book(title, author, pages, isChecked) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
this.isChecked = isChecked;
}

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 >= 1; n--) {
table.deleteRow(n);
}
Comment on lines 62 to 67

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.

Clearing <tbody> is easier and more efficient.

//insert updated row and cells
Expand All @@ -70,32 +78,28 @@ 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";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
}
changeBut.innerText = readStatus;
let readStatusBtn = document.createElement("button");
readStatusBtn.className = "btn btn-success";
wasReadCell.appendChild(readStatusBtn);
readStatusBtn.innerText = myLibrary[i].isChecked ? "Yes" : "No";

changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
readStatusBtn.addEventListener("click", function () {
myLibrary[i].isChecked = !myLibrary[i].isChecked;
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}`);

let removeBookBtn = document.createElement("button");

deleteCell.appendChild(removeBookBtn);
removeBookBtn.className = "btn btn-warning";
removeBookBtn.innerHTML = "Delete";
removeBookBtn.addEventListener("click", function () {
const toast = document.createElement("div");
toast.textContent = `You've deleted title: ${myLibrary[i].title}`;
table.appendChild(toast);
setTimeout(() => toast.remove(), 2000);
myLibrary.splice(i, 1);
render();
});
Expand Down
Loading