1+ var textAreaBorder = document . querySelector ( "#text-area" ) ;
2+ var textArea = document . querySelector ( "#text-area" ) ;
3+ var originalText = document . querySelector ( ".text-section-div p" ) . innerHTML ;
4+ var resetButton = document . querySelector ( "#reset" ) ;
5+ var theTimer = document . querySelector ( ".timer" ) ;
6+ var congSection = document . querySelector ( ".cong-section" ) ;
7+
8+ var timer = 0 ;
9+ var minutes = 0 ;
10+ var seconds = 0 ;
11+ var milliSeconds = 0 ;
12+ var currentTime = "" ;
13+ var interval = 0 ;
14+ var timerRunning = false ;
15+
16+ // Add leading zero to numbers 9 or below:
17+ function leadingZero ( time ) {
18+
19+ if ( time <= 9 ) {
20+ return "0" + time ;
21+ }
22+ else {
23+ return time ;
24+ }
25+ }
26+
27+ // Run a standard minute/second/hundredths timer:
28+ //minutes = Math.floor((timer/100)/60);
29+ //seconds = Math.floor((timer/100) - (minutes * 60));
30+ //milliSeconds = Math.floor(timer- (seconds * 100) - (minutes * 6000));
31+ function startTimer ( ) {
32+ minutes = Math . floor ( ( timer / 100 ) / 60 ) ;
33+ seconds = Math . floor ( ( timer / 100 ) - ( minutes * 60 ) ) ;
34+ milliSeconds = Math . floor ( timer - ( seconds * 100 ) - ( minutes * 6000 ) ) ;
35+
36+ minutes = leadingZero ( minutes ) ;
37+ seconds = leadingZero ( seconds ) ;
38+ milliSeconds = leadingZero ( milliSeconds ) ;
39+
40+ currentTime = minutes + ":" + seconds + ":" + milliSeconds ;
41+
42+ theTimer . innerHTML = currentTime ;
43+ timer ++ ;
44+ }
45+
46+
47+ // Match the text entered with the provided text on the page:
48+ function spellCheck ( ) {
49+ var textEntered = textArea . value ;
50+ var partialText = originalText . substring ( 0 , textEntered . length ) ;
51+
52+ if ( textEntered === originalText ) {
53+ textAreaBorder . style . borderColor = 'green' ;
54+ clearInterval ( interval ) ; // stop timer
55+ congSection . style . display = 'block' ; // display the congratulation message
56+ }
57+ else {
58+ if ( textEntered === partialText ) {
59+ textAreaBorder . style . borderColor = 'lightblue' ;
60+ }
61+ else {
62+ textAreaBorder . style . borderColor = 'red' ;
63+ }
64+ }
65+ }
66+
67+ // Start the timer:
68+ function start ( ) {
69+ var textEnteredLength = textArea . value . length ;
70+ if ( textEnteredLength === 0 && ! timerRunning ) {
71+ //Start The Timer
72+ interval = setInterval ( startTimer , 10 ) ;
73+ timerRunning = true ;
74+ }
75+ }
76+
77+ // Reset everything:
78+ function reset ( ) {
79+ clearInterval ( interval ) ;
80+ timer = 0 ;
81+ minutes = 0 ;
82+ seconds = 0 ;
83+ milliSeconds = 0 ;
84+ currentTime = "" ;
85+ interval = 0 ;
86+ timerRunning = false ;
87+ theTimer . innerHTML = "00:00:00" ;
88+ textArea . value = "" ;
89+ textAreaBorder . style . borderColor = 'gray' ;
90+ congSection . style . display = 'none' ; // hide the congratulations message
91+ }
92+
93+ // Event listeners for keyboard input and the reset button:
94+ textArea . addEventListener ( 'keypress' , start ) ;
95+ textArea . addEventListener ( 'keyup' , spellCheck ) ;
96+ resetButton . addEventListener ( 'click' , reset ) ;
0 commit comments