Skip to content

Commit 3635d7d

Browse files
book library project
1 parent 044affd commit 3635d7d

File tree

2 files changed

+39
-51
lines changed

2 files changed

+39
-51
lines changed

debugging/book-library/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h1>Library</h1>
6565
type="submit"
6666
value="Submit"
6767
class="btn btn-primary"
68-
onclick="submit();"
68+
onclick="addBook();"
6969
/>
7070
</div>
7171
</div>

debugging/book-library/script.js

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,40 @@
11
let myLibrary = [];
22

3-
window.addEventListener("load", function (e) {
3+
window.addEventListener("load", function () {
44
populateStorage();
5-
render();
65
});
76

87
function populateStorage() {
9-
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
8+
if (myLibrary.length === 0) {
9+
myLibrary.push(
10+
new Book("Robinson Crusoe", "Daniel Defoe", 252, true),
11+
new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true)
1612
);
17-
myLibrary.push(book1);
18-
myLibrary.push(book2);
19-
render();
2013
}
14+
render();
2115
}
2216

2317
const title = document.getElementById("title");
2418
const author = document.getElementById("author");
2519
const pages = document.getElementById("pages");
2620
const check = document.getElementById("check");
2721

28-
//check the right input from forms and if its ok -> add the new book (object in array)
29-
//via Book function and start render function
30-
function submit() {
31-
if (
32-
title.value == null ||
33-
title.value == "" ||
34-
pages.value == null ||
35-
pages.value == ""
36-
) {
22+
function addBook() {
23+
if (!title.value || !author.value || !pages.value) {
3724
alert("Please fill all fields!");
38-
return false;
39-
} else {
40-
let book = new Book(title.value, title.value, pages.value, check.checked);
41-
library.push(book);
42-
render();
25+
return;
4326
}
27+
28+
myLibrary.push(
29+
new Book(title.value, author.value, pages.value, check.checked)
30+
);
31+
32+
title.value = "";
33+
author.value = "";
34+
pages.value = "";
35+
check.checked = false;
36+
37+
render();
4438
}
4539

4640
function Book(title, author, pages, check) {
@@ -53,51 +47,45 @@ function Book(title, author, pages, check) {
5347
function render() {
5448
let table = document.getElementById("display");
5549
let rowsNumber = table.rows.length;
56-
//delete old table
57-
for (let n = rowsNumber - 1; n > 0; n-- {
50+
51+
52+
for (let n = rowsNumber - 1; n > 0; n--) {
5853
table.deleteRow(n);
5954
}
60-
//insert updated row and cells
61-
let length = myLibrary.length;
62-
for (let i = 0; i < length; i++) {
63-
let row = table.insertRow(1);
55+
56+
for (let i = 0; i < myLibrary.length; i++) {
57+
let row = table.insertRow(-1);
58+
6459
let titleCell = row.insertCell(0);
6560
let authorCell = row.insertCell(1);
6661
let pagesCell = row.insertCell(2);
6762
let wasReadCell = row.insertCell(3);
6863
let deleteCell = row.insertCell(4);
64+
6965
titleCell.innerHTML = myLibrary[i].title;
7066
authorCell.innerHTML = myLibrary[i].author;
7167
pagesCell.innerHTML = myLibrary[i].pages;
7268

73-
//add and wait for action for read/unread button
7469
let changeBut = document.createElement("button");
75-
changeBut.id = i;
7670
changeBut.className = "btn btn-success";
77-
wasReadCell.appendChild(changeBut);
78-
let readStatus = "";
79-
if (myLibrary[i].check == false) {
80-
readStatus = "Yes";
81-
} else {
82-
readStatus = "No";
83-
}
84-
changeBut.innerText = readStatus;
71+
changeBut.innerText = myLibrary[i].check ? "Yes" : "No";
8572

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

91-
//add delete button to every row and render again
92-
let delButton = document.createElement("button");
93-
delBut.id = i + 5;
94-
deleteCell.appendChild(delBut);
78+
wasReadCell.appendChild(changeBut);
79+
80+
let delBut = document.createElement("button");
9581
delBut.className = "btn btn-warning";
96-
delBut.innerHTML = "Delete";
97-
delBut.addEventListener("clicks", function () {
98-
alert(`You've deleted title: ${myLibrary[i].title}`);
82+
delBut.innerText = "Delete";
83+
84+
delBut.addEventListener("click", function () {
9985
myLibrary.splice(i, 1);
10086
render();
10187
});
88+
89+
deleteCell.appendChild(delBut);
10290
}
103-
}
91+
}

0 commit comments

Comments
 (0)