Skip to content

Commit 68519bd

Browse files
committed
add endpoint for raising to the power
1 parent 146b7b2 commit 68519bd

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

__tests__/utils/QueryProcessor.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ describe("QueryProcessor", () => {
4545
const response: string = QueryProcessor(query);
4646
expect(response).toBe("4096");
4747
});
48+
49+
test("should return subtraction result for minus query", () => {
50+
const query = "What is 120 minus 77?";
51+
const response: string = QueryProcessor(query);
52+
expect(response).toBe("43");
53+
});
54+
55+
test("should return primes from list", () => {
56+
const query = "Which of the following numbers are primes: 41, 85, 7, 59, 48?";
57+
const response: string = QueryProcessor(query);
58+
expect(response).toBe("41, 7, 59");
59+
});
4860
});

utils/QueryProcessor.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ export default function QueryProcessor(query: string): string {
4444
return (left - right).toString();
4545
}
4646

47+
const powerMatch = query
48+
.toLowerCase()
49+
.match(/what is\s*(-?\d+)\s*to the power of\s*(\d+)\?/);
50+
if (powerMatch) {
51+
const base = BigInt(powerMatch[1]);
52+
const exponent = Number(powerMatch[2]);
53+
let result = BigInt(1);
54+
for (let i = 0; i < exponent; i += 1) {
55+
result *= base;
56+
}
57+
return result.toString();
58+
}
59+
4760
const largestNumbersMatch = query
4861
.toLowerCase()
4962
.match(/which of the following numbers is the largest:\s*(.+)\?/);

0 commit comments

Comments
 (0)