-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (48 loc) · 1.49 KB
/
Copy pathscript.js
File metadata and controls
57 lines (48 loc) · 1.49 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
"use strict";
let myLibrary = [];
let bookData = [];
const bookForm = document.getElementById("add-book");
function Book(title, author, pages, read) {
(this.title = title),
(this.author = author),
(this.pages = pages),
(this.read = read);
}
function addBookToLibrary(title, author, pages, read) {
const aBook = new Book(title, author, pages, read);
myLibrary.push(aBook);
}
function showBook(bookLibrary) {
const bookCard = document.createElement("div");
bookCard.classList.add("book-card");
const lastItemMyLibrary = myLibrary[myLibrary.length - 1];
bookCard.textContent = `Title: ${lastItemMyLibrary.title} | Author: ${
lastItemMyLibrary.author
} | Pages: ${lastItemMyLibrary.pages} | ${
lastItemMyLibrary.read ? "READ" : "NOT READ"
}`;
let btn = document.createElement("button");
btn.innerHTML = "REMOVE";
bookCard.appendChild(btn);
btn.addEventListener("click", () => btn.parentElement.remove());
btn.classList.add("rmv-button");
document.querySelector(".book-show-panel").appendChild(bookCard);
changeColor(bookCard);
}
function changeColor(bookCard) {
if (bookCard.textContent.slice(-14, -11) == "NOT") {
bookCard.style.backgroundColor = "orange";
} else {
bookCard.style.backgroundColor = "green";
}
}
bookForm.addEventListener("submit", function (e) {
e.preventDefault();
const data = new FormData(bookForm);
for (const value of data) {
bookData.push(value[1]);
}
addBookToLibrary(...bookData);
bookData = [];
showBook();
});