Skip to content

Commit 7fb790a

Browse files
committed
More tolerant guess checking
1 parent df62a46 commit 7fb790a

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

src/App.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useEffect } from "react";
22

33
import styles from "./App.module.css";
44
import useAppState from "./useAppState";
5+
import normalizeString from "./util/normalizeString";
56

67
function App() {
78
const [state, dispatch] = useAppState();
@@ -12,10 +13,7 @@ function App() {
1213
.then((text) => {
1314
dispatch({
1415
type: "load-data",
15-
wordPack: text
16-
.split("\n")
17-
.map((word) => word.toUpperCase().trim())
18-
.filter(Boolean),
16+
wordPack: text.split("\n").map(normalizeString).filter(Boolean),
1917
});
2018
});
2119
}, [dispatch]);

src/useAppState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useReducer, type ActionDispatch } from "react";
22

33
import getRandomElement from "./util/getRandomElement";
4+
import normalizeString from "./util/normalizeString";
45

56
export type State = Readonly<
67
| {
@@ -66,7 +67,7 @@ export function reducer(state: State, action: Action): State {
6667
return state;
6768
}
6869

69-
if (action.newGuess === state.goal) {
70+
if (normalizeString(action.newGuess) === state.goal) {
7071
return {
7172
phase: "post-game",
7273
goal: state.goal,

src/util/normalizeString.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function normalizeString(s: string): string {
2+
return s.toUpperCase().trim().replaceAll(/\s+/g, " ");
3+
}

0 commit comments

Comments
 (0)