@@ -78,18 +78,28 @@ function initWords() {
7878 inputHistory = [ ] ;
7979 currentInput = "" ;
8080
81+ let language = words [ config . language ] ;
82+
83+ if ( language == undefined || language == [ ] ) {
84+ config . language = "english" ;
85+ language = words [ config . language ] ;
86+ }
87+
8188 if ( config . mode == "time" || config . mode == "words" ) {
8289
8390 let wordsBound = config . mode == "time" ? 50 : config . words ;
84- let randomWord = words [ Math . floor ( Math . random ( ) * words . length ) ] ;
85- wordsList . push ( randomWord ) ;
91+ let randomWord = language [ Math . floor ( Math . random ( ) * language . length ) ] ;
92+ while ( randomWord . indexOf ( ' ' ) > - 1 ) {
93+ randomWord = language [ Math . floor ( Math . random ( ) * language . length ) ] ;
94+ }
95+ wordsList . push ( randomWord . toLowerCase ( ) ) ;
8696 for ( let i = 1 ; i < wordsBound ; i ++ ) {
87- randomWord = words [ Math . floor ( Math . random ( ) * words . length ) ] ;
97+ randomWord = language [ Math . floor ( Math . random ( ) * language . length ) ] ;
8898 previousWord = wordsList [ i - 1 ] ;
89- while ( randomWord == previousWord && ( ! config . punctuation && "I" ) ) {
90- randomWord = words [ Math . floor ( Math . random ( ) * words . length ) ] ;
99+ while ( randomWord == previousWord || ( ! config . punctuation && randomWord == "I" ) || randomWord . indexOf ( ' ' ) > - 1 ) {
100+ randomWord = language [ Math . floor ( Math . random ( ) * language . length ) ] ;
91101 }
92- wordsList . push ( randomWord ) ;
102+ wordsList . push ( randomWord . toLowerCase ( ) ) ;
93103 }
94104
95105 } else if ( config . mode == "custom" ) {
@@ -98,7 +108,7 @@ function initWords() {
98108 wordsList . push ( w [ i ] ) ;
99109 }
100110 }
101- if ( config . punctuation ) {
111+ if ( config . punctuation && config . mode != "custom" ) {
102112 wordsList = buildSentences ( wordsList ) ;
103113 }
104114 showWords ( ) ;
@@ -108,13 +118,20 @@ function buildSentences() {
108118 let returnList = [ ] ;
109119 $ . each ( wordsList , ( index , word ) => {
110120 let previousWord = returnList [ index - 1 ] ;
111- if ( index == 0 || getLastChar ( previousWord ) == "." ) {
121+ if ( index == 0 || getLastChar ( previousWord ) == "." || getLastChar ( previousWord ) == "?" || getLastChar ( previousWord ) == "!" ) {
112122 //always capitalise the first word or if there was a dot
113123 word = capitalizeFirstLetter ( word ) ;
114124 } else if (
115- //10% chance to add a dot or if its a last word
125+ //10% chance to end a sentence
116126 ( Math . random ( ) < 0.1 && getLastChar ( previousWord ) != "." && index != wordsList . length - 2 ) || index == wordsList . length - 1 ) {
117- word += "." ;
127+ let rand = Math . random ( ) ;
128+ if ( rand <= 0.8 ) {
129+ word += "." ;
130+ } else if ( rand > .8 && rand < .9 ) {
131+ word += "?" ;
132+ } else {
133+ word += "!" ;
134+ }
118135 } else if ( Math . random ( ) < 0.01 &&
119136 getLastChar ( previousWord ) != "," &&
120137 getLastChar ( previousWord ) != "." ) {
@@ -144,7 +161,11 @@ function buildSentences() {
144161}
145162
146163function addWord ( ) {
147- let randomWord = words [ Math . floor ( Math . random ( ) * words . length ) ] ;
164+ let language = words [ config . language ]
165+ let randomWord = language [ Math . floor ( Math . random ( ) * language . length ) ] ;
166+ while ( randomWord . indexOf ( ' ' ) > - 1 ) {
167+ randomWord = language [ Math . floor ( Math . random ( ) * language . length ) ] ;
168+ }
148169 wordsList . push ( randomWord ) ;
149170 let w = "<div class='word'>" ;
150171 for ( let c = 0 ; c < randomWord . length ; c ++ ) {
@@ -378,7 +399,7 @@ function calculateStats() {
378399 let testNow = Date . now ( ) ;
379400 let testSeconds = ( testNow - testStart ) / 1000 ;
380401 let wpm = Math . round ( ( chars . correctWordChars * ( 60 / testSeconds ) ) / 5 ) ;
381- let acc = Math . round ( ( accuracyStats . correct / ( accuracyStats . correct + accuracyStats . incorrect ) ) * 100 ) ;
402+ let acc = Math . floor ( ( accuracyStats . correct / ( accuracyStats . correct + accuracyStats . incorrect ) ) * 100 ) ;
382403 return { wpm : wpm , acc : acc , correctChars : chars . allCorrectChars , incorrectChars : chars . incorrectChars + chars . extraChars + chars . missedChars } ;
383404}
384405
@@ -416,7 +437,8 @@ function showResult() {
416437 mode : config . mode ,
417438 mode2 : mode2 ,
418439 punctuation : config . punctuation ,
419- timestamp : Date . now ( )
440+ timestamp : Date . now ( ) ,
441+ language : config . language
420442 } ;
421443 if ( stats . wpm > 0 && stats . wpm < 250 && stats . acc > 50 && stats . acc <= 100 ) {
422444 if ( firebase . auth ( ) . currentUser != null ) {
@@ -445,15 +467,17 @@ function showResult() {
445467
446468
447469 let infoText = "" ;
448- infoText = config . mode ;
449470
471+
472+ infoText += config . mode ;
450473 if ( config . mode == "time" ) {
451474 infoText += " " + config . time
452475 } else if ( config . mode == "words" ) {
453476 infoText += " " + config . words
454477 }
478+ infoText += "<br>" + config . language . replace ( '_' , ' ' ) ;
455479 if ( config . punctuation ) {
456- infoText += " with punctuation"
480+ infoText += "<br> with punctuation"
457481 }
458482
459483 $ ( "#result .stats .info .bottom" ) . html ( infoText ) ;
@@ -829,6 +853,7 @@ $(document).keypress(function(event) {
829853 if ( ! $ ( "#wordsInput" ) . is ( ":focus" ) ) return ;
830854 if ( event [ "keyCode" ] == 13 ) return ;
831855 if ( event [ "keyCode" ] == 32 ) return ;
856+ if ( event [ "keyCode" ] == 27 ) return ;
832857 //start the test
833858 if ( currentInput == "" && inputHistory . length == 0 ) {
834859 if ( firebase . auth ( ) . currentUser != null ) {
@@ -1030,7 +1055,8 @@ let wpmOverTimeChart = new Chart(ctx, {
10301055 labelString : 'Words per Minute'
10311056 } ,
10321057 ticks : {
1033- fontFamily : 'Roboto Mono'
1058+ fontFamily : 'Roboto Mono' ,
1059+ beginAtZero : true
10341060 }
10351061 } ]
10361062 }
0 commit comments