forked from lomesh2312/Echo-Typist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (67 loc) · 2.26 KB
/
script.js
File metadata and controls
75 lines (67 loc) · 2.26 KB
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
const startBtn = document.getElementById('start-btn');
const gameArea = document.getElementById('game-area');
const wordDisplay = document.getElementById('word-display');
const userInput = document.getElementById('user-input');
const feedback = document.getElementById('feedback');
const scoreDisplay = document.getElementById('score');
const timerDisplay = document.getElementById('timer');
const words = ['hello', 'world', 'javascript', 'developer', 'game', 'speed', 'keyboard', 'audio', 'challenge', 'typing', 'Lomesh', 'Manik', 'Manik', 'echo', 'krushna', 'function', 'variable', 'future'];
let currentWord = '';
let score = 0;
let timeLeft = 30;
let gameInterval;
function getRandomWord() {
return words[Math.floor(Math.random() * words.length)];
}
function speakWord(word) {
const utterance = new SpeechSynthesisUtterance(word);
utterance.rate = Math.random() * (1.3 - 0.9) + 0.9;
utterance.pitch = 1;
utterance.lang = 'en-US';
speechSynthesis.speak(utterance);
}
function startGame() {
score = 0;
timeLeft = 30;
scoreDisplay.textContent = `Score: ${score}`;
timerDisplay.textContent = `Time Left: ${timeLeft}`;
startBtn.classList.add('hidden');
gameArea.classList.remove('hidden');
userInput.focus();
gameInterval = setInterval(() => {
timeLeft--;
timerDisplay.textContent = `Time Left: ${timeLeft}`;
if (timeLeft <= 0) {
clearInterval(gameInterval);
endGame();
}
}, 1000);
nextWord();
}
function nextWord() {
currentWord = getRandomWord();
wordDisplay.textContent = '🔊 Listen...';
setTimeout(() => speakWord(currentWord), 500);
}
userInput.addEventListener('input', () => {
if (userInput.value.trim().toLowerCase() === currentWord.toLowerCase()) {
feedback.textContent = '✅ Correct!';
feedback.style.color = '#00ff88';
score++;
scoreDisplay.textContent = `Score: ${score}`;
userInput.value = '';
nextWord();
} else {
feedback.textContent = '';
}
});
function endGame() {
feedback.textContent = `🎯 Time's up! Final Score: ${score}`;
feedback.style.color = '#fff';
startBtn.textContent = 'Play Again 🔁';
startBtn.classList.remove('hidden');
gameArea.classList.add('hidden');
wordDisplay.textContent = '';
userInput.value = '';
}
startBtn.addEventListener('click', startGame);