akochemasov/project-lvl1-s132

View on GitHub
src/games/gcd.js

Summary

Maintainability
A
0 mins
Test Coverage
import initGame from '..';
import { getRndInteger } from '../utils';

const description = 'Find the greatest common divisor of given numbers.';

const getNod = (a, b) => {
  if (b === 0) {
    return a;
  }
  return getNod(b, a % b);
};
const getCorrectAnswer = (question) => {
  const [a, b] = question.split(' ');
  return getNod(Number(a), Number(b));
};

const generateTask = () => {
  const question = `${getRndInteger(20)} ${getRndInteger(20)}`;
  const correctAnswer = getCorrectAnswer(question);
  return [question, String(correctAnswer)];
};

const game = () => {
  initGame(generateTask, description);
};

export default game;