Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
33 changes: 15 additions & 18 deletions debugging/book-library/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<!DOCTYPE html>
<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">
<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>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
/>
<link rel="stylesheet" type="text/css" href="style.css" />
>
<link rel="stylesheet" type="text/css" href="style.css" >
</head>

<body>
Expand All @@ -31,42 +28,42 @@ <h1>Library</h1>
<div class="form-group">
<label for="title">Title:</label>
<input
type="title"
type="text"
class="form-control"
id="title"
name="title"
required
/>
>
<label for="author">Author: </label>
<input
type="author"
type="text"
class="form-control"
id="author"
name="author"
required
/>
>
<label for="pages">Pages:</label>
<input
type="number"
class="form-control"
id="pages"
name="pages"
required
/>
>
<label class="form-check-label">
<input
type="checkbox"
class="form-check-input"
id="check"
value=""
/>Read
>Read
</label>
<input
id="submit-btn"
type="submit"
value="Submit"
class="btn btn-primary"
onclick="submit();"
/>
>
</div>
</div>

Expand All @@ -91,6 +88,6 @@ <h1>Library</h1>
</tbody>
</table>

<script src="script.js"></script>
<script src="script.js" type="module"></script>
</body>
</html>
66 changes: 40 additions & 26 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ let myLibrary = [];

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

function populateStorage() {
Expand All @@ -20,28 +19,45 @@ function populateStorage() {
}
}

const title = document.getElementById("title");
const author = document.getElementById("author");
const pages = document.getElementById("pages");
const check = document.getElementById("check");
const titleDom = document.getElementById("title");
const authorDom = document.getElementById("author");
const pagesDom = document.getElementById("pages");
const checkDom = document.getElementById("check");

document.getElementById('submit-btn').addEventListener('click', submit);

//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() {
const title = sanitize(titleDom.value.trim());
const author = sanitize(authorDom.value.trim());
const pages = sanitize(pagesDom.value.trim());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why convert the "special characters" in the input?

Copy link
Author

Choose a reason for hiding this comment

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

if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
title == "" ||
author == "" ||
pages == "" ||
parseInt(pages) < 0
) {
alert("Please fill all fields!");
alert("Please fill all fields or enter valid values!");
return false;
} else {
let book = new Book(title.value, title.value, pages.value, check.checked);
library.push(book);
let book = new Book(title, author, pages, checkDom.checked);
Copy link
Contributor

Choose a reason for hiding this comment

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

pages is still a string. A value like "3.14" or "3e1" could make past the check on line 38-39.

Copy link
Author

Choose a reason for hiding this comment

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

Now value converted to integer ( if possible).

myLibrary.push(book);
render();
}
}
function sanitize(string) {
const map = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
"/": '&#x2F;',
};
const reg = /[&<>"'/]/ig;
return string.replace(reg, (match)=>(map[match]));
}

function Book(title, author, pages, check) {
this.title = title;
Expand All @@ -52,11 +68,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-- {
table.deleteRow(n);
}
let tableBody = table.querySelector("tbody");
tableBody.textContent = '';
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
Expand All @@ -66,9 +80,9 @@ 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");
Expand All @@ -77,26 +91,26 @@ function render() {
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "Yes";
} else {
readStatus = "No";
} else {
readStatus = "Yes";
}
changeBut.innerText = readStatus;
changeBut.textContent = readStatus;
Comment on lines 78 to 98
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a good opportunity to practice using the ? : conditional operator. Can you rewrite the code on lines 92–98 as a single statement?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for advise. Now it refactored.


changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});

//add delete button to every row and render again
let delButton = document.createElement("button");
let delBut = 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}`);
delBut.textContent = "Delete";
delBut.addEventListener("click", function () {
myLibrary.splice(i, 1);
alert(`You've deleted title: ${myLibrary[i].title}`);
render();
});
}
Expand Down
2 changes: 1 addition & 1 deletion 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 Down