diff --git a/util/checkKeyword.test.ts b/util/checkKeyword.test.ts index 489409a..8b60dda 100644 --- a/util/checkKeyword.test.ts +++ b/util/checkKeyword.test.ts @@ -17,6 +17,10 @@ describe("Check Keyword", () => { describe("should return the index of the error if the keyword is wrong", () => { test("wrong character", () => { + expect(checkKeyword("ai", [["i"], ["a"]])).toBe(0) + expect(checkKeyword("ai", [["i"], ["i"]])).toBe(0) + expect(checkKeyword("ia", [["i"], ["i"]])).toBe(1) + expect(checkKeyword("aoi", [["a"], ["o"], ["i"]])).toBe(-1) expect(checkKeyword("eoi", [["a"], ["o"], ["i"]])).toBe(0) expect(checkKeyword("aei", [["a"], ["o"], ["i"]])).toBe(1) diff --git a/util/checkKeyword.ts b/util/checkKeyword.ts index ec62fa0..155eeaf 100644 --- a/util/checkKeyword.ts +++ b/util/checkKeyword.ts @@ -3,23 +3,17 @@ export const checkKeyword = ( pronounciation: string[][], ): number => { let checkIndex = 0 - let ok = true - for (let i = 0; i < pronounciation.length; i++) { + let ok = pronounciation.every((p) => { const rest = inputValue.slice(checkIndex) - for (let j = 0; j < pronounciation[i].length; j++) { - if (rest.length !== 0 && rest.startsWith(pronounciation[i][j])) { - checkIndex += pronounciation[i][j].length - ok = true - break - } + const found = p.find((p) => rest.startsWith(p)) - ok = false + if (!found) { + return false } - } - if (ok && checkIndex === inputValue.length) { - return -1 - } + checkIndex += found.length + return true + }) - return checkIndex + return ok && checkIndex === inputValue.length ? -1 : checkIndex }