This repository was archived by the owner on Dec 11, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
124 lines (104 loc) · 2.88 KB
/
Copy pathscript.js
File metadata and controls
124 lines (104 loc) · 2.88 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
124
// Word list
var words = [
"apple",
"banana",
"cherry",
"date",
"elderberry",
"fig",
"grape",
"honeydew",
"kiwi",
"lemon",
"mango",
"nectarine",
"orange",
"pear",
"quince",
"raspberry",
"strawberry",
"tangerine",
"watermelon",
"blueberry",
"blackberry",
"pineapple",
"apricot",
"coconut",
"papaya",
"grapefruit",
"lime",
"plum",
"pomegranate",
"rhubarb"
];
// Get timer element from the HTML
var timerElement = document.getElementById("timer");
var currentWordIndex = 0;
var score = 0;
var timeLeft = 60;
var userInput = document.getElementById("user-input");
var startButton = document.getElementById("btn");
userInput.disabled = true;
// Function to initialize the game
function init() {
showWord();
startTimer();
// Disable the start button and enable user input
startButton.disabled = true;
userInput.disabled = false;
// Clear the user input and display the initial score
userInput.value = "";
document.getElementById("score").textContent = "Score: " + score;
timerElement.style.color = "black";
timerElement.style.fontWeight = "normal";
// Add an event listener to check user input
userInput.addEventListener("input", checkInput);
}
// Function to display a random word
function showWord() {
// Generate a random index within the range of the word list array
currentWordIndex = Math.floor(Math.random() * words.length);
// Set the text content of the target element to the selected word
document.getElementById("target").textContent = words[currentWordIndex];
}
// Function to check user input
function checkInput() {
if (userInput.value === words[currentWordIndex]) {
score++;
// Update the score display
document.getElementById("score").textContent = "Score: " + score;
// Clear the user input
userInput.value = "";
showWord();
}
}
// Function to start the timer
function startTimer() {
timeLeft = 60;
// Update the timer display
timerElement.textContent = "Time: " + timeLeft;
// Start an interval to decrement the time left every second
var timerInterval = setInterval(function () {
timeLeft--;
// Update the timer display
timerElement.textContent = "Time: " + timeLeft;
// Change the timer color and style when 10 seconds are left
if (timeLeft === 10) {
timerElement.style.color = "red";
timerElement.style.fontWeight = "bold";
}
// End the game when the time runs out
if (timeLeft === 0) {
clearInterval(timerInterval);
gameOver();
}
}, 1000);
}
// Function to handle the game over scenario
function gameOver() {
document.getElementById("target").textContent = "Game Over";
// Disable user input and enable the start button
userInput.disabled = true;
startButton.disabled = false;
score = 0;
}