src/games/prime.js
import gameEngine from '..';
import getRandom from '../utils';
import { cons } from 'hexlet-pairs';
const condition = 'Answer "yes" if given number is prime. Otherwise answer "no".';
const isPrime = (num) => {
if (num < 2) {
return false;
}
if (num === 2) {
return true;
} if (num % 2 === 0) {
return false;
}
for (let i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i === 0) {
return false;
}
} return num > 2;
};
const generateQuestionAnswerPair = () => {
const question = getRandom(1, 100);
const correctAnswer = isPrime(question) ? 'yes' : 'no';
const data = cons(question, correctAnswer);
return data;
};
export default () => gameEngine(condition, generateQuestionAnswerPair);