-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
152 lines (110 loc) · 3.73 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
146
147
148
import data from "./data.json";
import JSConfetti from 'js-confetti'
import anime from 'animejs'
import 'regenerator-runtime/runtime'
const jsConfetti = new JSConfetti()
const randomItem = (array) => array[Math.floor(Math.random() * array.length)];
const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
const randomizeArray = (array) => array.sort(() => (Math.random() > .5) ? 1 : -1);
console.log(data.length);
// Fix game questions
let allQuestions = data
const notValid = allQuestions.filter(k => k.value === false)
let isValid = allQuestions.filter(k => k.value === true)
isValid = randomizeArray(isValid)
isValid = isValid.slice(0, notValid.length * 1.5);
let gameQuestions = randomizeArray(notValid.concat(isValid))
const createCard = () => {
const template = document.querySelector('.template').cloneNode(true)
template.classList.remove('template')
return template
}
const createNewCard = () => {
if (!gameQuestions.length) return
const cardEl = createCard()
const item = randomItem(gameQuestions)
gameQuestions = gameQuestions.filter(k => k !== item)
console.log(gameQuestions.length);
// Insert information
const nameEl = cardEl.querySelector('.description h2')
const extraEl = cardEl.querySelector('.description p')
// Fix long descirptions in name
if (item.name.includes('–')) {
const split = item.name.split('–')
item.name = split[0].trim()
item.extra = capitalizeFirstLetter(split[1].trim())
}
extraEl.innerText = item.extra ? item.extra : extraEl.remove()
nameEl.innerText = item.name
// Prepend Card
document.querySelector('main').prepend(cardEl)
// Actions
const buttons = cardEl.querySelectorAll('.actions button')
buttons.forEach(button => {
button.addEventListener('click', event => {
let answer = event.target.innerText
answer = answer === "Ja" ? true : false
const facit = answer === item.value
showAnswer(cardEl, facit, answer)
});
});
}
let answerdCount = 0
let scoreCount = 0
const updateScore = () => {
const subEl = document.querySelector('.sub')
if (subEl) subEl.remove()
const scoreEl = document.querySelector('.score')
scoreEl.style.display = 'inline'
const scoreCorrectEl = scoreEl.querySelector('.score-correct')
const scoreTotalEl = scoreEl.querySelector('.score-total')
scoreCorrectEl.innerText = scoreCount
scoreTotalEl.innerText = answerdCount
}
const showAnswer = async (el, facit, answer) => {
el.classList.add('answered')
const answerEl = el.querySelector('.answer')
const text = answerEl.querySelector('p')
answerdCount = answerdCount + 1
if (facit) scoreCount = scoreCount + 1
answerEl.classList.add(facit ? 'correct' : 'incorrect')
text.innerText = `${facit ? 'Rätt' : 'Fel'}, det kan man${answer === facit ? '' : ' inte'}`
updateScore()
if (facit) {
await jsConfetti.addConfetti({
emojis: ['✅', '💸', '🥇', '⭐️', '💚', '🏆', '🍾'],
confettiNumber: 120
})
createNewCard()
return
}
const xMax = 24;
anime({
targets: el,
easing: 'easeInOutSine',
duration: 400,
translateX: [
{
value: xMax * -1,
},
{
value: xMax,
},
{
value: xMax / -2,
},
{
value: xMax / 2,
},
{
value: 0
}
],
complete: function (anim) {
setTimeout(() => {
createNewCard()
}, 800)
}
});
}
createNewCard()