-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (50 loc) · 1.63 KB
/
app.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
const html = document.querySelector("html"),
modal = document.querySelector("dialog"),
openModal = document.querySelector("[name='open-modal']"),
closeModal = document.querySelector("[name='close-modal'"),
cards = document.querySelectorAll("article"),
toCompare = [null, null]
modal.addEventListener("close", () => html.classList.remove("modal-is-open", "modal-is-opening"))
openModal.addEventListener("click", () => {
html.classList.add("modal-is-open", "modal-is-opening")
modal.showModal()
})
closeModal.addEventListener("click", () => modal.close())
cards.forEach(card => {
card.addEventListener("mousedown", () => {
if (toCompare[0] == card) {
toCompare[0] = null;
removeSelection()
return;
}
card.setAttribute("class", "card-selected")
if (toCompare[0] == null) toCompare[0] = card
else {
toCompare[1] = card
compare()
removeSelection()
resetComparition()
}
})
})
function compare() {
const firstBook = toCompare[0].dataset,
secondBook = toCompare[1].dataset
let output = [
"Title: They are ",
"Author: They are writed by ",
`Number of Pages: ${firstBook.pages}`
]
output[0] += firstBook.title === secondBook.title ? "the same book" : "different books"
output[1] += firstBook.author === secondBook.author? "the same author" : "different authors"
output[2] += (
+firstBook.pages > +secondBook.pages ? " > " :
+firstBook.pages < +secondBook.pages ? " < " : " = "
) + secondBook.pages
alert(output.join("\n"))
}
function resetComparition() { toCompare[0] = null; toCompare[1] = null }
function removeSelection() {
toCompare[0].removeAttribute("class")
toCompare[1].removeAttribute("class")
}