Show a proper timer

This commit is contained in:
2025-02-15 19:30:03 +01:00
parent d3c6a4e024
commit f617689d2b
3 changed files with 12 additions and 3 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ function Code({code, loaded}: CodeProps) {
const incorrectlyTyped = characters.filter(char => !char).length; const incorrectlyTyped = characters.filter(char => !char).length;
if (timer > 0) { if (timer > 0) {
setScore((typed / 5 - incorrectlyTyped) / (timer / 60)); setScore(Math.abs(typed / 5 - incorrectlyTyped) / (timer / 60));
setAccuracy(correctlyTyped / typed * 100); setAccuracy(correctlyTyped / typed * 100);
} }
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
+3 -2
View File
@@ -1,12 +1,13 @@
import { useTypingContext } from "contexts/typing"; import { useTypingContext } from "contexts/typing";
import './index.css'; import './index.css';
import { renderTimer } from "./utils";
type StatsProps = { type StatsProps = {
loaded: boolean; loaded: boolean;
} }
function Score({loaded}: StatsProps) { function Score({ loaded }: StatsProps) {
const { const {
timer, timer,
score, score,
@@ -17,7 +18,7 @@ function Score({loaded}: StatsProps) {
return ( return (
<div className='score'> <div className='score'>
<p>Time: {timer}</p> {renderTimer(timer)}
<p>WPM: {Math.round(score)}</p> <p>WPM: {Math.round(score)}</p>
<p>Accuracy: {Math.round(accuracy)}%</p> <p>Accuracy: {Math.round(accuracy)}%</p>
</div> </div>
+8
View File
@@ -0,0 +1,8 @@
export function renderTimer(timer: number) {
const leftValue = Math.round(timer / 60);
const rightValue = timer < 60 ? timer : timer % 60;
return (
<p>Time: {leftValue < 10 && 0}{leftValue}:{rightValue < 10 && 0}{rightValue}</p>
);
}