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;
if (timer > 0) {
setScore((typed / 5 - incorrectlyTyped) / (timer / 60));
setScore(Math.abs(typed / 5 - incorrectlyTyped) / (timer / 60));
setAccuracy(correctlyTyped / typed * 100);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
+2 -1
View File
@@ -1,6 +1,7 @@
import { useTypingContext } from "contexts/typing";
import './index.css';
import { renderTimer } from "./utils";
type StatsProps = {
loaded: boolean;
@@ -17,7 +18,7 @@ function Score({loaded}: StatsProps) {
return (
<div className='score'>
<p>Time: {timer}</p>
{renderTimer(timer)}
<p>WPM: {Math.round(score)}</p>
<p>Accuracy: {Math.round(accuracy)}%</p>
</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>
);
}