Skip to content

Commit a070da4

Browse files
committed
add a few more endpoints
1 parent 8ffc866 commit a070da4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

utils/QueryProcessor.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ export default function QueryProcessor(query: string): string {
4747
return (left * middle + right).toString();
4848
}
4949

50+
const plusMultiplyMatch = query
51+
.toLowerCase()
52+
.match(/what is\s*(-?\d+(?:\.\d+)?)\s*plus\s*(-?\d+(?:\.\d+)?)\s*multiplied by\s*(-?\d+(?:\.\d+)?)\?/);
53+
if (plusMultiplyMatch) {
54+
const left = Number(plusMultiplyMatch[1]);
55+
const middle = Number(plusMultiplyMatch[2]);
56+
const right = Number(plusMultiplyMatch[3]);
57+
return (left + middle * right).toString();
58+
}
59+
5060
const minusMatch = query
5161
.toLowerCase()
5262
.match(/what is\s*(-?\d+(?:\.\d+)?)\s*minus\s*(-?\d+(?:\.\d+)?)\?/);
@@ -141,5 +151,35 @@ export default function QueryProcessor(query: string): string {
141151
}
142152
}
143153

154+
const scrabbleScoreMatch = query
155+
.toLowerCase()
156+
.match(/what is the scrabble score of\s*([a-z]+)\?/);
157+
if (scrabbleScoreMatch) {
158+
const word = scrabbleScoreMatch[1];
159+
const letterScores: Record<string, number> = {
160+
a: 1, b: 3, c: 3, d: 2, e: 1, f: 4, g: 2, h: 4, i: 1, j: 8, k: 5, l: 1, m: 3,
161+
n: 1, o: 1, p: 3, q: 10, r: 1, s: 1, t: 1, u: 1, v: 4, w: 4, x: 8, y: 4, z: 10
162+
};
163+
164+
const score = word
165+
.split("")
166+
.reduce((total, letter) => total + (letterScores[letter] || 0), 0);
167+
return score.toString();
168+
}
169+
170+
const anagramMatch = query
171+
.toLowerCase()
172+
.match(/which of the following is an anagram of\s*([a-z]+):\s*(.+)\?/);
173+
if (anagramMatch) {
174+
const target = anagramMatch[1];
175+
const choices = anagramMatch[2].split(",").map((value) => value.trim());
176+
177+
const normalizedTarget = target.split("").sort().join("");
178+
const matchingChoice = choices.find((choice) => choice.split("").sort().join("") === normalizedTarget);
179+
if (matchingChoice) {
180+
return matchingChoice;
181+
}
182+
}
183+
144184
return "";
145185
}

0 commit comments

Comments
 (0)