-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-reverse.js
106 lines (89 loc) · 2.75 KB
/
text-reverse.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
//------Https------\\
if (window.location.protocol != "https:") {
window.location.protocol="https:";
}
const block = document.querySelector(".block");
const text = document.querySelector(".text");
const input = document.querySelector(".input");
const btn = document.getElementById("btnValider");
const answer = document.querySelector(".answer");
const lost = document.querySelector(".lost");
const scoreTxt = document.querySelector(".score");
const timerTxt = document.querySelector(".timer");
const scoreBestTxt = document.querySelector(".scoreBest");
const popup = document.querySelector(".popup");
const popupBtn = document.getElementById("btnOk");
const wordArray = words.trim().split('\n');
let score = 0;
let scoreBest = localStorage.getItem("scoreBest") ? parseInt(localStorage.getItem("scoreBest")) : 0;
let word;
let timer;
let countdown;
const delay = 10000;
scoreBestTxt.textContent = "Meilleur score: " + scoreBest;
function newWord() {
const rdmWord = Math.floor(Math.random() * wordArray.length);
word = wordArray[rdmWord];
let wordReverse = reverseWord(word);
text.textContent = wordReverse;
clearTimeout(timer);
clearInterval(countdown);
let timeRemaining = delay / 1000;
timerTxt.textContent = "Temps restant: " + timeRemaining;
countdown = setInterval(() => {
timeRemaining--;
timerTxt.textContent = "Temps restant: " + timeRemaining;
if (timeRemaining <= 0) {
clearInterval(countdown);
if (score > 0) {
score--;
}
scoreTxt.textContent = "Score: " + score;
text.textContent = word;
input.value = "";
answer.style.display = "none";
lost.style.display = "block";
setTimeout(() => {
newWord();
answer.style.display = "block";
lost.style.display = "none";
}, 3000);
}
}, 1000);
}
popupBtn.addEventListener("click", function() {
popup.style.display = "none";
block.style.filter = "blur(0px)";
newWord();
});
function reverseWord(word) {
let array = word.split('');
do {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
} while (array.join('') === word);
return array.join('');
}
function correctWord() {
if (input.value.toLowerCase() === word) {
score++;
scoreTxt.textContent = "Score: " + score;
input.value = "";
if (score > scoreBest) {
scoreBest = score;
scoreBestTxt.textContent = "Meilleur score: " + scoreBest;
localStorage.setItem("scoreBest", scoreBest);
}
newWord();
}
}
btn.addEventListener("click", function() {
correctWord();
});
input.addEventListener("keydown", function(event) {
if (event.key === "Enter") {
correctWord();
}
});