RuslanFahrenheit/frontend-project-lvl1

View on GitHub
src/games/prime.js

Summary

Maintainability
A
40 mins
Test Coverage
import getRandomInt from '../getRandomInt';
import { makeGame } from '../api';
import buildGame from '..';

const isPrime = (n) => {
  if (n < 2) {
    return false;
  }

  for (let i = 2; i < n / 2; i += 1) {
    if (n % i === 0) {
      return false;
    }
  }

  return true;
};
const min = 1;
const max = 100;
const description = 'Answer "yes" if given number is prime. Otherwise answer "no".';

const createGameData = () => {
  const task = getRandomInt(min, max);
  const correctAnswer = isPrime(task) ? 'yes' : 'no';

  return makeGame(task, correctAnswer);
};

export default () => buildGame(createGameData, description);