-
Notifications
You must be signed in to change notification settings - Fork 5
/
quizzes.js
65 lines (55 loc) · 2.16 KB
/
quizzes.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
import { quizzes, images, color } from "./utils.js"
class Quizzes {
constructor() {
this.cardsContainer = document.querySelector(".quizzes-main")
this.card = document.createElement("a")
this.image = document.createElement("img")
this.label = document.createElement("p")
this.title = document.createElement("h2")
this.date = document.createElement("span")
this.javascript = "JavaScript"
this.css = "CSS"
this.createCards()
}
setAttributes(el, attrs) {
for(var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
getImageByCard(quizTechnology){
const packageImages = images.filter(image => {
if(image.categories.includes(quizTechnology)){
return image;
}
});
return packageImages
}
setColorByQuizzTechnology(quizTechnology){
switch (quizTechnology) {
case this.javascript:
this.cardContainer.getElementsByTagName("p")[0].style.backgroundColor = color.SHALIMARA;
break;
case this.css:
this.cardContainer.getElementsByTagName("p")[0].style.backgroundColor = color.CARNATION_PINK;
break;
default:
this.cardContainer.getElementsByTagName("p")[0].style.backgroundColor = color.AQUAMARINE;
break;
}
}
createCards() {
quizzes.forEach(quiz => {
const imagesByCategoryQuiz = this.getImageByCard(quiz.technology);
const randomImage = Math.floor(Math.random() * imagesByCategoryQuiz.length);
this.cardContainer = this.cardsContainer.appendChild(this.card.cloneNode(true))
this.cardContainer.setAttribute("href", quiz.url)
this.cardContainer.setAttribute("target", "_blank")
this.setAttributes(this.cardContainer.appendChild(this.image.cloneNode(true)), {"src": imagesByCategoryQuiz[randomImage].src, "alt": imagesByCategoryQuiz[randomImage].alt})
this.cardContainer.appendChild(this.label.cloneNode(true)).innerHTML = quiz.technology
this.cardContainer.appendChild(this.title.cloneNode(true)).innerHTML = quiz.name
this.cardContainer.appendChild(this.date.cloneNode(true)).innerHTML = quiz.date
this.setColorByQuizzTechnology(quiz.technology);
})
}
}
new Quizzes()