ulanivan/project-lvl1-s474

View on GitHub
src/games/prime.js

Summary

Maintainability
A
0 mins
Test Coverage
import getRandom from '../utils';
import core from '..';

const descriptionTask = 'Answer "yes" if given number is prime. Otherwise answer "no"';

const isPrime = (num) => {
  if (num < 2) return false;
  for (let i = 2; i < num / 2; i += 1) {
    if (num % i === 0) return false;
  }
  return true;
};

const getTask = () => {
  const question = getRandom(1, 3572);
  const correctAnswer = isPrime(question) ? 'yes' : 'no';
  return { question, correctAnswer };
};

export default () => {
  core(getTask, descriptionTask);
};