-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (136 loc) · 4.39 KB
/
script.js
File metadata and controls
156 lines (136 loc) · 4.39 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const quoteApiUrl = "https://api.quotable.io/random?minLength=150&maxLength=200";
const quoteSection = document.getElementById("quote");
const userInput = document.getElementById("quote-input");
let quote = "";
let time = 0;
let timer = "";
let mistakes = 0;
var wpmVal
var accuracyVal
var dataValues
const renderNewQuote = async () => {
try {
const response = await fetch(quoteApiUrl);
if (!response.ok) {
throw new Error('Network response was not ok');
}
let data = await response.json();
quote = data.content;
} catch (error) {
console.error("Failed to fetch quote from API:", error);
// Fetch from local JSON if API fails
const localResponse = await fetch("/quotes.json");
const localData = await localResponse.json();
const randomIndex = Math.floor(Math.random() * localData.quotes.length);
quote = localData.quotes[randomIndex];
}
let arr = quote.split("").map((value) => {
return "<span class='quote-chars'>" + value + "</span>";
});
quoteSection.innerHTML = arr.join("");
};
if(userInput){
userInput.addEventListener("input", () => {
let quoteChars = document.querySelectorAll(".quote-chars");
quoteChars = Array.from(quoteChars);
let userInputChars = userInput.value.split("");
quoteChars.forEach((char, index) => {
if (char.innerText == userInputChars[index]) {
char.classList.add("success");
}
else if (userInputChars[index] == null) {
if (char.classList.contains("success")) {
char.classList.remove("success");
} else {
char.classList.remove("fail");
}
}
else {
if (!char.classList.contains("fail")) {
mistakes += 1;
char.classList.add("fail");
}
document.getElementById("mistakes").innerText = mistakes;
}
let check = quoteChars.every((element) => {
return element.classList.contains("success");
});
if (check) {
displayResult();
}
});
});
}
function updateTimer() {
document.getElementById("timer").innerText = ++time + "s";
}
const timeReduce = () => {
timer = setInterval(updateTimer, 1000);
};
const displayResult = () => {
document.querySelector(".result").style.display = "block";
clearInterval(timer);
document.getElementById("stop-test").style.display = "none";
userInput.disabled = true;
timeTaken = (time) / 60;
wpmVal = parseInt((userInput.value.length / 5 / timeTaken).toFixed(2))
accuracyVal = Math.round(
((userInput.value.length - mistakes) / userInput.value.length) * 100
)
document.getElementById("wpm").innerText =
wpmVal + " wpm";
document.getElementById("accuracy").innerText =
accuracyVal + " %"
dataValues = [time, accuracyVal, mistakes, wpmVal];
console.log(dataValues)
localStorage.setItem('myData', JSON.stringify(dataValues));
window.location.href = 'result.html';
};
const startTest = () => {
mistakes = 0;
timer = "";
userInput.disabled = false;
userInput.focus();
timeReduce();
document.getElementById("start-test").style.display = "none";
document.getElementById("stop-test").style.display = "block";
};
if(userInput){
userInput.addEventListener('keydown', function(event) {
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight' || event.key === 'ArrowUp' || event.key === 'ArrowDown') {
event.preventDefault();
}
});
}
document.addEventListener('click', function() {
userInput.focus();
});
window.onload = () => {
userInput.value = "";
document.getElementById("start-test").style.display = "block";
document.getElementById("stop-test").style.display = "none";
userInput.disabled = true;
renderNewQuote();
};
let valueDisplays = document.querySelectorAll(".num");
let interval = 4000;
valueDisplays.forEach((valueDisplay, index) => {
var storedDataString = localStorage.getItem('myData');
dataValues = JSON.parse(storedDataString);
console.log(dataValues)
let startValue = 0;
let endValue = dataValues[index];
if(endValue!=0){
let duration = Math.floor(interval / endValue);
let counter = setInterval(function () {
startValue += 1;
valueDisplay.textContent = startValue;
if (startValue == endValue) {
clearInterval(counter);
}
}, duration);
}
else{
valueDisplay.textContent = endValue;
}
});