From 26d6d4721e3a05abf8c7720408fa06f85460297d Mon Sep 17 00:00:00 2001 From: Ali Javed <148910960+Alijavedofficial@users.noreply.github.com> Date: Thu, 16 May 2024 00:30:50 +0500 Subject: [PATCH] Fix:Updated the WPM calculation logic Updated the WPM calculation logic to ensure accurate calculation based on the time taken and word count. Fixed issues with startTime and endTime initialization and ensured proper state updates for calculating WPM. --- src/App.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 664d8e3..07c2b94 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -88,13 +88,20 @@ export default function App() { setInput(newValue); }; - + +//Updated the WPM calculation logic to ensure accurate calculation based on the time taken and word count. Fixed issues with startTime and endTime initialization and ensured proper state updates for calculating WPM. const calculateWPM = () => { if (!startTime || !endTime) return 0; - const timeTaken = (endTime - startTime) / 60000; - const wordCount = currentText.split(" ").length; - return (wordCount / timeTaken).toFixed(2); + const timeTaken = (endTime - startTime) / 60000; + const wordCount = input.trim().match(/\S+/g)?.length || 0; + return (wordCount / timeTaken).toFixed(0); }; + + useEffect(() => { + if (isCompleted) { + setEndTime(Date.now()); // Set endTime when typing is completed + } + }, [isCompleted]); const calculatePoints = (mistakes: number) => { const textLength = currentText.replace(/\s/g, "").length; @@ -211,3 +218,4 @@ export default function App() { ); } +