Skip to content

Commit 0215f2a

Browse files
committed
feat: add query for largest numbers and addition
1 parent 2fb571b commit 0215f2a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

__tests__/utils/QueryProcessor.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@ describe("QueryProcessor", () => {
3232
expect(response).toBe((
3333
"mcadena"
3434
));
35-
})
35+
});
36+
37+
test("should return sum for plus query", () => {
38+
const query = "What is 21 plus 96?";
39+
const response: string = QueryProcessor(query);
40+
expect(response).toBe("117");
41+
});
3642
});

utils/QueryProcessor.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,28 @@ export default function QueryProcessor(query: string): string {
1717
return "mcadena";
1818
}
1919

20+
const plusMatch = query
21+
.toLowerCase()
22+
.match(/what is\s*(-?\d+(?:\.\d+)?)\s*plus\s*(-?\d+(?:\.\d+)?)\?/);
23+
if (plusMatch) {
24+
const left = Number(plusMatch[1]);
25+
const right = Number(plusMatch[2]);
26+
return (left + right).toString();
27+
}
28+
29+
const largestNumbersMatch = query
30+
.toLowerCase()
31+
.match(/which of the following numbers is the largest:\s*(.+)\?/);
32+
if (largestNumbersMatch) {
33+
const numbers = largestNumbersMatch[1]
34+
.split(",")
35+
.map((value) => Number(value.trim()))
36+
.filter((value) => !Number.isNaN(value));
37+
38+
if (numbers.length > 0) {
39+
return Math.max(...numbers).toString();
40+
}
41+
}
42+
2043
return "";
2144
}

0 commit comments

Comments
 (0)