-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (78 loc) · 2.25 KB
/
script.js
File metadata and controls
80 lines (78 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const myLibrary = [];
const table = document.querySelector(".list");
const addBtn = document.querySelector(".add");
addBtn.addEventListener("click", function () {
addBookToLibrary(
document.querySelector("#title").value,
document.querySelector("#author").value,
document.querySelector("#pages").value,
document.querySelector("#read").value
);
console.log(myLibrary);
displayBooks();
});
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.id = crypto.randomUUID();
}
Book.prototype.info = function () {
return `${this.title} by ${this.author}, ${this.pages} pages, ${
this.read ? "already read" : "not read yet"
}.`;
};
function addBookToLibrary(title, author, pages, read) {
myLibrary.push(new Book(title, author, pages, read));
}
function displayNumbers() {
const number = document.querySelectorAll(".number");
number.forEach((v, i) => {
v.textContent = i + 1;
});
}
function displayBooks() {
while (table.firstChild) {
table.removeChild(table.firstChild);
}
myLibrary.forEach((v, i) => {
const aBook = document.createElement("tr");
const nCell = document.createElement("td");
const titleCell = document.createElement("td");
const authorCell = document.createElement("td");
const pagesCell = document.createElement("td");
const readCell = document.createElement("td");
const idCell = document.createElement("td");
const rmBtnCell = document.createElement("td");
const rmBtn = document.createElement("button");
idCell.classList.add("id");
nCell.classList.add("number");
titleCell.textContent = v.title;
authorCell.textContent = v.author;
pagesCell.textContent = v.pages;
readCell.textContent = v.read;
idCell.textContent = v.id;
rmBtn.textContent = "Remove";
rmBtn.addEventListener("click", function () {
myLibrary.splice(
myLibrary.findIndex((v) => v.id === idCell.textContent),
1
);
aBook.remove();
displayNumbers();
});
rmBtnCell.append(rmBtn);
aBook.append(
nCell,
titleCell,
authorCell,
pagesCell,
readCell,
idCell,
rmBtnCell
);
table.append(aBook);
});
displayNumbers();
}