maximtop/project-lvl1-s17

View on GitHub
src/games/prime.js

Summary

Maintainability
A
40 mins
Test Coverage
import { getRandomNumber } from '../utils';
import brainGames from '../brain-games';

const min = 1;
const max = 100;

const isRightGameInput = answer => answer === 'yes' || answer === 'no';

const isPrime = (number) => {
  let start = 2;
  while (start <= Math.sqrt(number)) {
    if (number % start < 1) {
      return false;
    }
    start += 1;
  }
  return number > 1;
};

const getGame = () => {
  const gameQuestion = getRandomNumber(min, max);
  const gameAnswer = isPrime(gameQuestion) ? 'yes' : 'no';
  return [gameQuestion, gameAnswer, isRightGameInput];
};

export default () => {
  const gameDescription = 'Answer "yes" if number is prime otherwise answer "no"';
  brainGames(gameDescription, getGame);
};