Skip to content

Commit fda2bc0

Browse files
committed
fix input types, improve variable naming, and clean up script.js
1 parent 2f60517 commit fda2bc0

2 files changed

Lines changed: 32 additions & 48 deletions

File tree

‎debugging/book-library/index.html‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="en">
33
<head>
44
<title>My Book Library</title>
5-
<meta
6-
charset="utf-8"
7-
name="viewport"
8-
content="width=device-width, initial-scale=1.0"
9-
/>
5+
<meta charset="utf-8"/>
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
107
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
118
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
129
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
@@ -31,15 +28,15 @@ <h1>Library</h1>
3128
<div class="form-group">
3229
<label for="title">Title:</label>
3330
<input
34-
type="title"
31+
type="text"
3532
class="form-control"
3633
id="title"
3734
name="title"
3835
required
3936
/>
4037
<label for="author">Author: </label>
4138
<input
42-
type="author"
39+
type="text"
4340
class="form-control"
4441
id="author"
4542
name="author"
@@ -91,6 +88,6 @@ <h1>Library</h1>
9188
</tbody>
9289
</table>
9390

94-
<script src="script.js"></script>
91+
<script src="script.js" type="module"></script>
9592
</body>
9693
</html>

‎debugging/book-library/script.js‎

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let myLibrary = [];
1+
const myLibrary = [];
22

33
window.addEventListener("load", function (e) {
44
populateStorage();
@@ -7,39 +7,30 @@ window.addEventListener("load", function (e) {
77

88
function populateStorage() {
99
if (myLibrary.length == 0) {
10-
let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
11-
let book2 = new Book(
12-
"The Old Man and the Sea",
13-
"Ernest Hemingway",
14-
"127",
15-
true
16-
);
10+
const book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
11+
const book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", "127", true);
1712
myLibrary.push(book1);
1813
myLibrary.push(book2);
19-
render();
2014
}
2115
}
2216

23-
const title = document.getElementById("title");
24-
const author = document.getElementById("author");
25-
const pages = document.getElementById("pages");
26-
const check = document.getElementById("check");
17+
const titleInput = document.getElementById("title");
18+
const authorInput = document.getElementById("author");
19+
const pagesInput = document.getElementById("pages");
20+
const checkInput = document.getElementById("check");
2721

2822
//check the right input from forms and if its ok -> add the new book (object in array)
2923
//via Book function and start render function
3024
function submit() {
3125
if (
32-
author.value == null ||
33-
author.value == "" ||
34-
title.value == null ||
35-
title.value == "" ||
36-
pages.value == null ||
37-
pages.value == ""
26+
authorInput.value == "" ||
27+
titleInput.value == "" ||
28+
pagesInput.value == ""
3829
) {
3930
alert("Please fill all fields!");
4031
return false;
4132
} else {
42-
let book = new Book(title.value, author.value, pages.value, check.checked);
33+
const book = new Book(titleInput.value, authorInput.value, Number(pagesInput.value), checkInput.checked);
4334
myLibrary.push(book);
4435
render();
4536
}
@@ -53,52 +44,48 @@ function Book(title, author, pages, check) {
5344
}
5445

5546
function render() {
56-
let table = document.getElementById("display");
57-
let rowsNumber = table.rows.length;
47+
const table = document.getElementById("display");
48+
const rowsNumber = table.rows.length;
5849
//delete old table
5950
for (let n = rowsNumber - 1; n > 0; n--) {
6051
table.deleteRow(n);
6152
}
6253
//insert updated row and cells
63-
let length = myLibrary.length;
54+
const length = myLibrary.length;
6455
for (let i = 0; i < length; i++) {
65-
let row = table.insertRow(1);
66-
let titleCell = row.insertCell(0);
67-
let authorCell = row.insertCell(1);
68-
let pagesCell = row.insertCell(2);
69-
let wasReadCell = row.insertCell(3);
70-
let deleteCell = row.insertCell(4);
56+
const row = table.insertRow(1);
57+
const titleCell = row.insertCell(0);
58+
const authorCell = row.insertCell(1);
59+
const pagesCell = row.insertCell(2);
60+
const wasReadCell = row.insertCell(3);
61+
const deleteCell = row.insertCell(4);
7162
titleCell.innerHTML = myLibrary[i].title;
7263
authorCell.innerHTML = myLibrary[i].author;
7364
pagesCell.innerHTML = myLibrary[i].pages;
7465

7566
//add and wait for action for read/unread button
76-
let changeBut = document.createElement("button");
67+
const changeBut = document.createElement("button");
7768
changeBut.id = i;
7869
changeBut.className = "btn btn-success";
7970
wasReadCell.appendChild(changeBut);
80-
let readStatus = "";
81-
if (myLibrary[i].check == false) {
82-
readStatus = "No";
83-
} else {
84-
readStatus = "Yes";
85-
}
86-
changeBut.innerText = readStatus;
71+
72+
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
8773

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

9379
//add delete button to every row and render again
94-
let delBut = document.createElement("button");
80+
const delBut = document.createElement("button");
9581
delBut.id = i + 5;
9682
deleteCell.appendChild(delBut);
9783
delBut.className = "btn btn-warning";
9884
delBut.innerHTML = "Delete";
9985
delBut.addEventListener("click", function () {
100-
alert(`You've deleted title: ${myLibrary[i].title}`);
86+
const deletedTitle = myLibrary[i].title;
10187
myLibrary.splice(i, 1);
88+
alert(`You've deleted title: ${deletedTitle}`);
10289
render();
10390
});
10491
}

0 commit comments

Comments
 (0)