Skip to content

Commit 112509b

Browse files
committed
add endpoint for squares and cubes
1 parent 0215f2a commit 112509b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

utils/QueryProcessor.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,29 @@ export default function QueryProcessor(query: string): string {
4040
}
4141
}
4242

43+
const squareAndCubeMatch = query
44+
.toLowerCase()
45+
.match(/which of the following numbers is both a square and a cube:\s*(.+)\?/);
46+
if (squareAndCubeMatch) {
47+
const numbers = squareAndCubeMatch[1]
48+
.split(",")
49+
.map((value) => Number(value.trim()))
50+
.filter((value) => Number.isInteger(value));
51+
52+
const matchingNumbers = numbers.filter((value) => {
53+
if (value < 0) {
54+
return false;
55+
}
56+
57+
const squareRoot = Math.round(Math.sqrt(value));
58+
const cubeRoot = Math.round(Math.cbrt(value));
59+
return squareRoot * squareRoot === value && cubeRoot * cubeRoot * cubeRoot === value;
60+
});
61+
62+
if (matchingNumbers.length > 0) {
63+
return matchingNumbers[0].toString();
64+
}
65+
}
66+
4367
return "";
4468
}

0 commit comments

Comments
 (0)