Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added change #10

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions utils/QueryProcessor.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
import { isPrimitive } from "util";

function isSquareAndCube(number: number) {
const squareRoot = Math.sqrt(number);
const cubeRoot = Math.cbrt(number);

return Number.isInteger(squareRoot) && Number.isInteger(cubeRoot);
}

function isPrime(num: number) {
for (let i = 2; i < num; i++) {
if (Number.isInteger(num / i)) return false;
}

return true
}

export default function QueryProcessor(query: string): string {
if (query.toLowerCase().includes("shakespeare")) {
return (
"William Shakespeare (26 April 1564 - 23 April 1616) was an " +
"English poet, playwright, and actor, widely regarded as the greatest " +
"writer in the English language and the world's pre-eminent dramatist."
);
} else if (query.toLowerCase().includes("name")) {
return "Jam";
} else if (query.includes("Which of the following numbers is the largest:")) {
let i = query.indexOf(":");
let j = query.indexOf("?");
let s = query.substring(i + 1, j);
let parts = s.split(", ");
let nums = parts.map(e => parseInt(e))
let maxi = nums.reduce((acc, cur) => Math.max(acc, cur));
return maxi.toString();
} else if (query.includes("What is") && query.includes("plus")) {
let strs = query.match(/\d+/g)!;
let numbers = strs.map(Number);
return (numbers.reduce((acc, cur) => acc + cur).toString());
} else if (query.includes("What is") && query.includes("multiplied by")) {
let strs = query.match(/\d+/g)!;
let numbers = strs.map(Number);
return (numbers.reduce((acc, cur) => acc * cur).toString());
} else if (query.includes("Which of the following numbers is both a square and a cube:")) {
let strs = query.match(/\d+/g)!;
let numbers = strs.map(Number);
return (numbers.filter(e => isSquareAndCube(e)).toString());
} else if (query.includes("Which of the following numbers are primes:")) {
let strs = query.match(/\d+/g)!;
let numbers = strs.map(Number);
numbers = numbers.filter(e => isPrime(e));
return numbers.toString();
} else if (query.includes("What is ") && query.includes("minus")) {
let strs = query.match(/\d+/g)!;
let numbers = strs.map(Number);
let num = numbers[0]-numbers[1];
return num.toString();
}

return "";
Expand Down