Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
35 changes: 16 additions & 19 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"
type="text"
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>
48 changes: 23 additions & 25 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,25 +19,26 @@ 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() {
if (
title.value == null ||
title.value == "" ||
pages.value == null ||
pages.value == ""
titleDom.value == "" ||
authorDom.value == "" ||
pagesDom.value == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the type of the pagesDom.value? Why compare it to 0?

Copy link
Author

Choose a reason for hiding this comment

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

Changed to ==" "

) {
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(titleDom.value.trim(), authorDom.value.trim(), pagesDom.value, 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.

The values you used on line 40 are not the values you checked on lines 33-35. A book may end up having an empty string as its title or a negative number as its page count.

A better practice is
Step 1: Sanitised and preprocessed the raw input values and stored the resulting value in some variables.
Step 2; Check the values in the variables
Step 3: If the values are ok, use the values stored in the variables.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I've changed it.

myLibrary.push(book);
render();
}
}
Expand All @@ -52,11 +52,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 +64,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 +75,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