@@ -4,30 +4,92 @@ var originalText = document.querySelector(".text-section-div p").innerHTML;
44var resetButton = document . querySelector ( "#reset" ) ;
55var theTimer = document . querySelector ( ".timer" ) ;
66
7+ var timer = 0 ;
8+ var minutes = 0 ;
9+ var seconds = 0 ;
10+ var milliSeconds = 0 ;
11+ var currentTime = "" ;
12+ var interval = 0 ;
13+ var timerRunning = false ;
714
815// Add leading zero to numbers 9 or below:
9-
16+ function leadingZero ( time ) {
17+ if ( time <= 9 ) {
18+ return "0" + time ;
19+ }
20+ else {
21+ return time ;
22+ }
23+ }
1024
1125
1226// Run a standard minute/second/hundredths timer:
1327//minutes = Math.floor((timer/100)/60);
1428//seconds = Math.floor((timer/100) - (minutes * 60));
1529//milliSeconds = Math.floor(timer- (seconds * 100) - (minutes * 6000));
30+ function startTimer ( ) {
31+ minutes = Math . floor ( ( timer / 100 ) / 60 ) ;
32+ seconds = Math . floor ( ( timer / 100 ) - ( minutes * 60 ) ) ;
33+ milliSeconds = Math . floor ( timer - ( seconds * 100 ) - ( minutes * 6000 ) ) ;
1634
35+ minutes = leadingZero ( minutes ) ;
36+ seconds = leadingZero ( seconds ) ;
37+ milliSeconds = leadingZero ( milliSeconds ) ;
1738
39+ currentTime = minutes + ":" + seconds + ":" + milliSeconds ;
40+ theTimer . innerHTML = currentTime ;
41+ timer ++ ;
42+ }
1843
1944
20- // Match the text entered with the provided text on the page:
2145
46+ // Match the text entered with the provided text on the page:
47+ function spellCheck ( ) {
48+ var textEntered = textArea . value ;
49+ var partialString = originalText . substring ( 0 , textEntered . length ) ;
50+ if ( textEntered == originalText ) {
51+ textAreaBorder . style . borderColor = "green" ;
52+ clearInterval ( interval ) ;
53+ }
54+ else {
55+ if ( textEntered == partialString ) {
56+ textAreaBorder . style . borderColor = "blue" ;
57+ }
58+ else {
59+ textAreaBorder . style . borderColor = "red" ;
60+ }
61+ }
62+
63+ }
2264
2365
2466
2567// Start the timer:
26-
68+ function start ( ) {
69+ var textEnteredLength = textArea . value . length ;
70+ if ( textEnteredLength === 0 && ! timerRunning ) {
71+ interval = setInterval ( startTimer , 10 ) ;
72+ timerRunning = true ;
73+ }
74+ }
2775
2876
2977// Reset everything:
30-
78+ function reset ( ) {
79+ timer = 0 ;
80+ minutes = 0 ;
81+ seconds = 0 ;
82+ milliSeconds = 0 ;
83+ currentTime = "" ;
84+ interval = 0 ;
85+ timerRunning = false ;
86+ clearInterval ( interval ) ;
87+ theTimer . innerHTML = "00:00:00" ;
88+ textAreaBorder . style . borderColor = "gray" ;
89+ textArea . value = "" ;
90+ }
3191
3292// Event listeners for keyboard input and the reset button:
33-
93+ textArea . addEventListener ( 'keypress' , start ) ;
94+ textArea . addEventListener ( 'keyup' , spellCheck ) ;
95+ resetButton . addEventListener ( 'click' , reset ) ;
0 commit comments