-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (136 loc) · 4.01 KB
/
script.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const addbutton = document.getElementById("addbutton");
addbutton.addEventListener("click", (e) => {
modalcontainer.classList.add("show");
});
const cancelbtn = document.getElementById("cancelbtn");
cancelbtn.addEventListener("click", (e) => {
modalcontainer.classList.remove("show");
});
const info = document.getElementById("info");
function infoclick() {
if (info.textContent == "show info") {
feedatacontainerid.classList.add("show");
info.textContent = "hide info";
console.log(info.textContent);
} else {
feedatacontainerid.classList.remove("show");
info.textContent = "show info";
console.log("hi");
}
}
const addBtn = document.querySelector("#addbtn");
addBtn.addEventListener("click", () => {
if (
form.title.value &&
form.author.value &&
form.pages.value &&
form.compee.value
) {
if (+form.pages.value >= +form.compee.value) addBookToLibrary();
else {
alert("Total pages must be greater than the completed pages");
}
} else {
alert("Fill All Input Fields");
}
});
function set(p, q) {
feedbooks = document.getElementById("books");
feedbooks.textContent = myLibrary.length;
feedtd = document.getElementById("tp");
feedcp = document.getElementById("comp");
if (myLibrary.length > 0) {
const res = p.reduce(
(accumulator, currentValue) => +accumulator + +currentValue
);
feedtd.textContent = res;
const res1 = q.reduce(
(accumulator, currentValue) => +accumulator + +currentValue
);
feedcp.textContent = res1;
} else {
feedtd.textContent = 0;
feedcp.textContent = 0;
}
}
class book {
constructor(title, author, pages, compee) {
this.title = form.title.value;
this.author = form.author.value;
this.pages = form.pages.value;
this.compee = form.compee.value;
}
}
let myLibrary = [];
let newbook;
function addBookToLibrary() {
modalcontainer.classList.remove("show");
newbook = new book(title, author, pages, compee);
myLibrary.unshift(newbook);
render();
}
function render() {
const display = document.getElementById("Library-container");
const books = document.querySelectorAll(".book");
books.forEach((book) => display.removeChild(book));
for (let i = 0; i < myLibrary.length; i++) {
createBook(myLibrary[i]);
}
var some = [];
var some1 = [];
myLibrary.map((item) => {
some.push(item.pages);
});
myLibrary.map((item) => {
some1.push(item.compee);
});
set(some, some1);
}
function rerender(k) {
myLibrary.splice(k, 1);
const display = document.getElementById("Library-container");
const books = document.querySelectorAll(".book");
books.forEach((book) => display.removeChild(book));
for (let i = 0; i < myLibrary.length; i++) {
createBook(myLibrary[i]);
}
var some = [];
var some1 = [];
myLibrary.map((item) => {
some.push(item.pages);
});
myLibrary.map((item) => {
some1.push(item.compee);
});
set(some, some1);
}
function createBook(item) {
const library = document.querySelector("#Library-container");
const bookDiv = document.createElement("div");
const titleDiv = document.createElement("div");
const authDiv = document.createElement("div");
const pageDiv = document.createElement("div");
const compeeDiv = document.createElement("div");
const removeBtn = document.createElement("button");
bookDiv.classList.add("book");
bookDiv.setAttribute("id", myLibrary.indexOf(item));
titleDiv.textContent = item.title;
titleDiv.classList.add("title");
bookDiv.appendChild(titleDiv);
authDiv.textContent = item.author;
authDiv.classList.add("author");
bookDiv.appendChild(authDiv);
pageDiv.textContent = item.pages;
pageDiv.classList.add("pages");
bookDiv.appendChild(pageDiv);
compeeDiv.textContent = item.compee;
compeeDiv.classList.add("compee");
bookDiv.appendChild(compeeDiv);
removeBtn.textContent = "Delete";
removeBtn.classList.add("rbtn");
bookDiv.appendChild(removeBtn);
library.appendChild(bookDiv);
removeBtn.addEventListener("click", () => {
rerender(myLibrary.indexOf(item));
});
}