cududoma/project-lvl1-s232

View on GitHub
src/games/game-gcd.js

Summary

Maintainability
A
0 mins
Test Coverage
import { getRandomInt, commonGameLogic } from '../index';

const gcd = (firstTermExp, secondTermExp) => {
  let a = firstTermExp;
  let b = secondTermExp;
  while (a !== 0 && b !== 0) {
    if (a > b) {
      a %= b;
    } else {
      b %= a;
    }
  }
  return a + b;
};

const gcdGameCondition = () => {
  const firstTermExp = getRandomInt(1, 99);
  const secondTermExp = getRandomInt(1, 99);
  const question = `${firstTermExp} ${secondTermExp}`;
  const answer = gcd(firstTermExp, secondTermExp);
  return [question, answer];
};

const gcdGame = () => {
  commonGameLogic('Find the greatest common divisor of given numbers', gcdGameCondition);
};

export default gcdGame;