From 093436b2197db41b63ff09de369c7ca7643a2a13 Mon Sep 17 00:00:00 2001 From: Voxel Date: Sun, 13 Apr 2025 20:41:43 -0400 Subject: [PATCH] Create script.js --- type/script.js | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 type/script.js diff --git a/type/script.js b/type/script.js new file mode 100644 index 0000000..79f7132 --- /dev/null +++ b/type/script.js @@ -0,0 +1,111 @@ +const textToTypeElement = document.getElementById('text-to-type'); +const userInput = document.getElementById('user-input'); +const timerDisplay = document.getElementById('timer'); +const speedDisplay = document.getElementById('speed'); +const accuracyDisplay = document.getElementById('accuracy'); +const restartButton = document.getElementById('restart-btn'); + +let timer; +let seconds = 0; +let typedText = ''; +let totalWords = 0; +let correctWords = 0; +let isTyping = false; + +const texts = [ + "the quick brown fox jumps over the lazy dog", + "oh the misery everybody wants to be my enemy", + "she said do you love me I tell her only partly", + "this sample text is sponsored by wavesmiley.com", + "i got two phones one for the plug and one for the load", + "whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune", + "free nvidia geforce now ultraviolet fortnite proxy working february 2023", + "fuck a matthew crook I could never miss a target", + "croissant burglar never bluegrass premium gambit folding", + "dodecahedron mixtape groan fort chicken mountains", + "who's that? is he some sort of king? no, he's a legend", + "i've never had dinner with the president", + "water ice salt aye meep jacket leather coral", + "you scream I scream we all scream for okay", + "can I get a rizz and can you make it gyatt forever", + "soup vacuum insane wily grumbling vampire", + "habanero world island corn platinum triple zoom pebbles", + "pineapple backflip front side lefty righty zero forty one", + "eight seven tricks four troll deer fawn four", + "nine glue grass ground floor chips man woman horse no", + "mandy's candies we all love mandy's candies", + "flop moop jop grimb lich hop eleventyseven fish bart", + "frimp tomorrow forever popsicle evangelion humanity crystals", + "instead of drinking imported beers somebody brought a bottle of orphan tears", + "this mailbox is mine and this triagonal sign", + "milknet glaze i am milknet by playboi milky", + "limbo by mindcap and more", + "github lotus flower chinese bottom ninety", + "i told her i'm big like bieber she ain't believe me", + "it sounds like a lot of hoopla lemon lime cake" +]; + +function getRandomText() { + const randomIndex = Math.floor(Math.random() * texts.length); + return texts[randomIndex]; +} + +function setTextToType() { + const randomText = getRandomText(); + textToTypeElement.innerText = randomText; +} + +function startTest() { + setTextToType(); + userInput.value = ''; + seconds = 0; + timerDisplay.innerText = seconds; + speedDisplay.innerText = 0; + accuracyDisplay.innerText = 100; + isTyping = false; + clearInterval(timer); +} + +function countCorrectWords(input, text) { + const inputWords = input.split(' '); + const textWords = text.split(' '); + let correctCount = 0; + for (let i = 0; i < inputWords.length; i++) { + if (inputWords[i] === textWords[i]) { + correctCount++; + } + } + return correctCount; +} + +function updateStats() { + const totalWordsTyped = typedText.split(' ').length; + correctWords = countCorrectWords(typedText, textToTypeElement.innerText); + const speed = Math.round((totalWordsTyped / 5) / (seconds / 60)); + const accuracy = Math.round((correctWords / totalWordsTyped) * 100); + + speedDisplay.innerText = speed; + accuracyDisplay.innerText = accuracy; +} + +userInput.addEventListener('input', function() { + if (!isTyping) { + startTimer(); + isTyping = true; + } + + typedText = userInput.value; + totalWords = typedText.split(' ').length; + updateStats(); +}); + +function startTimer() { + timer = setInterval(function() { + seconds++; + timerDisplay.innerText = seconds; + }, 1000); +} + +restartButton.addEventListener('click', startTest); + +startTest();