-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
40 lines (32 loc) · 1.07 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
const text = document.getElementById('direction');
const translation = document.getElementById('translation');
const correctBtn = document.getElementById('correct');
let gameStarted = false;
let wordset;
let wordIndex;
let previousIndex;
fetch('./wordset.json')
.then(response => response.json())
.then(data => (wordset = data));
const showElement = className =>
className.forEach(x => x.classList.remove('hide'));
const hideElement = className =>
className.forEach(x => x.classList.add('hide'));
const randomWord = () => Math.floor(Math.random() * wordset.length);
const checkDirection = dir => {
if (!gameStarted) return;
if (dir === wordset[wordIndex].direction) {
showElement([translation, correctBtn]);
}
};
const generateWord = () => {
// Prevent word from appearing twice in a row
do {
wordIndex = randomWord();
} while (wordIndex === previousIndex);
text.innerText = wordset[wordIndex].word;
translation.innerText = wordset[wordIndex].translation;
hideElement([translation, correctBtn]);
previousIndex = wordIndex;
gameStarted = true;
};