|
| 1 | +const bttn = document.querySelector('#test-click') |
| 2 | +const inputArea = document.querySelector('#input-field') |
| 3 | +const paragraphBox = document.querySelector("#paragraph-box") |
| 4 | +const startBttn = document.querySelector('#start-bttn') |
| 5 | +const wpm_container = document.querySelector('#wpm_con') |
| 6 | +const time_container = document.querySelector('#time_con') |
| 7 | +const accuracy_container = document.querySelector('#acc_con') |
| 8 | + |
| 9 | +let index_no = 0 |
| 10 | +let mainArray = [] |
| 11 | +let spanArray = [] |
| 12 | +let modArray = [] |
| 13 | +let typedChar |
| 14 | +let r_time = 0 |
| 15 | +let curr_input = '' |
| 16 | +let middle_char = 30 |
| 17 | +let scroll_amnt = 10 |
| 18 | +let curr_input_array = [] |
| 19 | +let latest_arr_count = 0 |
| 20 | + |
| 21 | +let char_typedCount = 0 |
| 22 | +let char_typedWrong = 0 |
| 23 | +let char_typedCorrect = 0 |
| 24 | + |
| 25 | +let timer |
| 26 | +let count = 0 |
| 27 | +let array_count = 0 |
| 28 | + |
| 29 | +let typedChar_arr = [] |
| 30 | +let charCheckWrong = false |
| 31 | + |
| 32 | + |
| 33 | +const words = [ |
| 34 | + "JavaScript is the only language that I'm aware of that people feel they don't need to learn before they start using it.", |
| 35 | + "Be humble, communicate clearly, and respect others. It costs nothing to be kind, but the impact is priceless.", |
| 36 | + "If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.", |
| 37 | + "I make mistakes because I'm always operating at my limit. If I only stay in comfortable territory all the time, that's not so much fun.", |
| 38 | + "If you learn how to solve problems, you can go through life and do pretty well.", |
| 39 | + "To be successful, you want to surround yourself with very talented folks whose skills blend very well. That's the secret of success.", |
| 40 | + "Good judgement comes from experience. Experience comes from bad judgement." |
| 41 | +] |
| 42 | + |
| 43 | +function again() { |
| 44 | + resetVal() |
| 45 | + inputArea.focus() |
| 46 | + startBttn.innerHTML = 'Again' |
| 47 | + init() |
| 48 | + check() |
| 49 | + time_counter() |
| 50 | +} |
| 51 | + |
| 52 | +function resetVal() { |
| 53 | + paragraphBox.scrollLeft = 0; |
| 54 | + clearInterval(timer) |
| 55 | + inputArea.value = '' |
| 56 | + latest_arr_count = 0; |
| 57 | + time_container.innerText = '0' |
| 58 | + wpm_container.innerText = '' |
| 59 | + accuracy_container.innerText = '' |
| 60 | + char_typedWrong = 0 |
| 61 | + count = 0 |
| 62 | + charCheckWrong = false |
| 63 | + array_count = 0 |
| 64 | + typedChar_arr = [] |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +function focus_now() { |
| 69 | + inputArea.focus() |
| 70 | +} |
| 71 | + |
| 72 | +function start() { |
| 73 | + startBttn.innerHTML = 'Start' |
| 74 | + inputArea.value = '' |
| 75 | + resetVal() |
| 76 | + paragraphBox.innerHTML = "Press Start button to begin......" |
| 77 | +} |
| 78 | + |
| 79 | +function randomWords() { |
| 80 | + return Math.floor(Math.random() * words.length) |
| 81 | +} |
| 82 | + |
| 83 | +function init() { |
| 84 | + paragraphBox.innerHTML = '' |
| 85 | + words[randomWords()].split('').forEach(ch => { |
| 86 | + const span_char = document.createElement('span') |
| 87 | + span_char.innerText = ch |
| 88 | + paragraphBox.appendChild(span_char) |
| 89 | + // array_count += 1 |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +function changeSpace(s) { |
| 94 | + if (s == ' ') { |
| 95 | + s = ' ' |
| 96 | + } |
| 97 | + return s |
| 98 | +} |
| 99 | + |
| 100 | +function create_Array() { |
| 101 | + curr_input = inputArea.value |
| 102 | + curr_input_array = curr_input.split('').map(changeSpace) |
| 103 | + |
| 104 | + spanArray = paragraphBox.querySelectorAll('span') |
| 105 | + spanArray.forEach(e => { |
| 106 | + if (e.innerHTML == ' ') { |
| 107 | + e.innerHTML = ' ' |
| 108 | + } |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +let index_char = -1 |
| 113 | +function check() { |
| 114 | + create_Array() |
| 115 | + |
| 116 | + let s = [] |
| 117 | + spanArray.forEach(e => { |
| 118 | + s.push(e.innerHTML) |
| 119 | + }) |
| 120 | + |
| 121 | + spanArray[0].classList.add('cursor-highlight') |
| 122 | + |
| 123 | + spanArray.forEach((char, index) => { |
| 124 | + typedChar = curr_input_array[index] |
| 125 | + |
| 126 | + // Cheking if latest char is the same with the text |
| 127 | + if (typedChar == null) { //If the test have not yet started |
| 128 | + char.classList.remove('wrong') |
| 129 | + char.classList.remove('correct') |
| 130 | + } else if (typedChar == char.innerHTML) { //If the char typed match |
| 131 | + char.classList.remove('wrong') |
| 132 | + char.classList.add('correct') |
| 133 | + charCheckWrong = false |
| 134 | + // typedChar_arr[index_char] = 1 |
| 135 | + } else if (typedChar != char.innerHTML) { //If the char do not match |
| 136 | + char.classList.remove('correct') |
| 137 | + char.classList.add('wrong') |
| 138 | + charCheckWrong = true |
| 139 | + // typedChar_arr[index_char] = 0 |
| 140 | + } |
| 141 | + |
| 142 | + // Highlighting the wrong and cursor-highlight |
| 143 | + if (index == curr_input_array.length) { |
| 144 | + if (char === ' ') char.classList.add('space-wrong') |
| 145 | + char.classList.add('cursor-highlight') |
| 146 | + } else char.classList.remove('cursor-highlight') |
| 147 | + |
| 148 | + if (curr_input_array.length == spanArray.length) { |
| 149 | + // TODO display the Stats |
| 150 | + clearInterval(timer) |
| 151 | + char_typedWrong = determine_wrong_counts() |
| 152 | + |
| 153 | + console.log(char_typedWrong, "= wrong counts") |
| 154 | + calculate_wpm() |
| 155 | + calculate_acc() |
| 156 | + // startBttn.classList.remove('hidden') |
| 157 | + startBttn.focus() |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + typedChar_arr.push(charCheckWrong) |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +function calculate_acc() { |
| 166 | + let acc = Math.round(((spanArray.length - char_typedWrong) / (spanArray.length)) * 100) |
| 167 | + accuracy_container.innerText = acc + '%' |
| 168 | +} |
| 169 | + |
| 170 | +function time_counter() { |
| 171 | + timer = setInterval(() => { |
| 172 | + count++ |
| 173 | + time_container.innerText = count |
| 174 | + }, 1002) |
| 175 | +} |
| 176 | + |
| 177 | +function calculate_wpm() { |
| 178 | + let wpm_score = Math.round(((spanArray.length) / 5) / (count / 60)) |
| 179 | + wpm_container.innerText = wpm_score |
| 180 | +} |
| 181 | + |
| 182 | +function determine_wrong_counts() { |
| 183 | + let typedWrong = 0 |
| 184 | + for (let i = 0; i < typedChar_arr.length; i++) { |
| 185 | + if (typedChar_arr[i] == true) { |
| 186 | + typedWrong += 1 |
| 187 | + } |
| 188 | + } |
| 189 | + return typedWrong |
| 190 | +} |
| 191 | + |
| 192 | +function ignored_Key(e_key) { |
| 193 | + const ignore_keys = { |
| 194 | + 'Shift': 0, |
| 195 | + 'Alt': 0, |
| 196 | + 'Control': 0, |
| 197 | + } |
| 198 | + return ignore_keys[e_key] ?? -1 |
| 199 | +} |
| 200 | + |
| 201 | +let scroll_actual_val = 0 |
| 202 | +inputArea.addEventListener('keydown', (e) => { |
| 203 | + let name = e.key |
| 204 | + if (name == 'Backspace') { |
| 205 | + if (curr_input_array.length > spanArray.length * 0.90) { |
| 206 | + paragraphBox.scrollLeft -= 0 |
| 207 | + } else { |
| 208 | + paragraphBox.scrollLeft -= spanArray.length / 10 |
| 209 | + } |
| 210 | + } |
| 211 | + else if (ignored_Key(name) == 0) paragraphBox.scrollLeft += 0 |
| 212 | + else { |
| 213 | + // scroll_left_val += spanArray.length / 10 |
| 214 | + paragraphBox.scrollLeft += spanArray.length / 10 |
| 215 | + scroll_actual_val += spanArray.length / 10 |
| 216 | + } |
| 217 | + |
| 218 | +}, false) |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +start() |
| 223 | + |
0 commit comments