-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (107 loc) · 3.66 KB
/
app.js
File metadata and controls
123 lines (107 loc) · 3.66 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
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
// List of alphabets A-Z
const alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
// Variables to track state
let currentIndex = 0;
let startTime = null;
let endTime = null;
let warningSound = new Audio(
"https://assets.mixkit.co/active_storage/sfx/946/946-preview.mp3"
);
let successSound = new Audio(
"https://assets.mixkit.co/active_storage/sfx/2861/2861-preview.mp3"
);
let startSound = new Audio(
"https://assets.mixkit.co/active_storage/sfx/2574/2574-preview.mp3"
);
let warningInterval = null;
let isTestEnded = false;
let isStarted = false;
const alphabetDisplay = document.getElementById("alphabet-display");
const timerElement = document.getElementById("timer");
const resultElement = document.getElementById("result");
const warningElement = document.getElementById("warning");
const restartButton = document.getElementById("restart-button");
// Start the timer
function startTimer() {
startTime = new Date();
timerInterval = setInterval(() => {
if (!isTestEnded) {
const elapsedTime = Math.round((new Date() - startTime) / 1000);
timerElement.innerText = `Time Taken: ${elapsedTime} seconds`;
}
}, 1000);
}
// Stop the timer
function stopTimer() {
clearInterval(timerInterval);
}
// Handle key presses
function handleKeyPress(event) {
const keyPressed = event.key.toLowerCase();
if (isTestEnded) return; // Ignore input if test is ended
if (keyPressed === alphabet[currentIndex]) {
// Start timer and play start sound if it's the first key
if (currentIndex === 0 && !isStarted) {
startSound.play();
startTimer();
isStarted = true;
}
// Update display
alphabetDisplay.innerText = keyPressed.toUpperCase();
document.getElementById(keyPressed).classList.add("correct");
// Move to the next letter
currentIndex++;
// Check if the sequence is complete
if (currentIndex === alphabet.length) {
endTime = new Date();
const timeTaken = Math.round((endTime - startTime) / 1000); // Time in seconds
resultElement.innerText = `You completed the sequence in ${timeTaken} seconds!`;
resultElement.style.display = "block"; // Show result
alphabetDisplay.innerText = "";
stopTimer();
successSound.play();
isTestEnded = true;
restartButton.style.display = "block"; // Show restart button
document.removeEventListener("keydown", handleKeyPress);
}
} else {
// Handle incorrect key press
warningElement.innerText =
"Incorrect key pressed. Please press the correct key.";
// Start or continue warning sound
if (warningSound.paused) {
warningSound.play();
} else {
warningSound.currentTime = 0;
}
// Stop warning sound when the correct key is pressed
if (warningInterval) {
clearInterval(warningInterval);
}
warningInterval = setInterval(() => {
if (keyPressed === alphabet[currentIndex]) {
warningSound.pause();
warningSound.currentTime = 0;
warningElement.innerText = "";
document.getElementById(keyPressed).classList.remove("incorrect");
clearInterval(warningInterval);
}
}, 500);
// Highlight incorrect button
document.getElementById(keyPressed).classList.add("incorrect");
}
}
// Add key buttons to the page
alphabet.forEach((letter) => {
const button = document.createElement("button");
button.id = letter;
button.className = "key-button";
button.innerText = letter.toUpperCase();
document.body.appendChild(button);
});
// Attach event listener
document.addEventListener("keydown", handleKeyPress);
// Restart game
restartButton.addEventListener("click", () => {
location.reload(); // Refresh the page to restart the game
});