-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (68 loc) · 2.87 KB
/
script.js
File metadata and controls
78 lines (68 loc) · 2.87 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
// map constants to sections of the document
const testWrapper = document.querySelector(".test-wrapper");
const testArea = document.querySelector("#test-area");
const originText = document.querySelector("#origin-text p").innerHTML;
const resetButton = document.querySelector("#reset");
const theTimer = document.querySelector(".timer");
// global variable
var timer = [0,0,0,0]; // array of minutes, seconds, hundredths of seconds, thousandths of seconds
var interval;
var timerRunning = false;
// Add leading zero to numbers 9 or below (purely for aesthetics):
function leadingZero(time) {
"use strict";
if (time <= 9) {
time = "0" + time;
}
return time;
}
// Run a standard minute/second/hundredths timer:
function runTimer() {
"use strict";
theTimer.innerHTML = leadingZero(timer[0]) + ":" + leadingZero(timer[1]) + ":" + leadingZero(timer[2]);
timer[3]++; // only increment the last element in the array (thousandths of seconds)
timer[0] = Math.floor((timer[3]/100)/60); // no decimals
timer[1] = Math.floor((timer[3]/100) - (timer[0] * 60));
timer[2] = Math.floor(timer[3] - (timer[1] * 100) - (timer[0] * 6000));
}
// Match the text entered with the provided text on the page:
function spellCheck() {
"use strict";
let textEntered = testArea.value;
let originTextMatch = originText.substring(0, textEntered.length);
if (textEntered === originText) { // if all is entered and it matches, then test is done.
clearInterval(interval); // stops the continuous checking (similar to chron job)
testWrapper.style.borderColor = "#429890"; // set to green color
} else {
if (textEntered === originTextMatch) { // if the text entered so far is correct...
testWrapper.style.borderColor = "#65CCf3"; // set to blue color
} else { // if incorrect (mistakes made)
testWrapper.style.borderColor = "#E95D0F"; // set to orange color
}
}
}
// Start the timer:
function start() {
"use strict";
let textEnteredLength = testArea.value.length;
if (textEnteredLength === 0 && !timerRunning) {
timerRunning = true;
interval = setInterval(runTimer, 10); // 10 = every thousandth of a second; similar to chron job
}
console.log(textEnteredLength);
}
// Reset everything:
function reset() {
"use strict";
clearInterval(interval); // ensures that browser isn't running an interval in the background and tying up resources
interval = null; // this prevents a different interval from being made the next time it is run
timer = [0,0,0,0]; // reset / re-initialize the timer array
timerRunning = false; // reset timerRunning boolean flag
testArea.value = "";
theTimer.innerHTML = "00:00:00";
testWrapper.style.borderColor = "grey";
}
// Event listeners for keyboard input and the reset button:
testArea.addEventListener("keypress", start, false);
testArea.addEventListener("keyup", spellCheck, false);
resetButton.addEventListener("click", reset, false);