Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
fairwind2k committed Jul 29, 2023
1 parent 0eb17d8 commit 346c1b7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/games/gcd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ const max = 100;

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

const getCommonDivisor = () => {
const arr = [];
let [a, b] = pairs(min, max);
arr.push(`${String(a)} ${String(b)}`);
const getCommonDivisor = (num1, num2) => {
let a = num1;
let b = num2;
while (a !== 0 && b !== 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
const divisor = a + b;
arr.push(String(divisor));
return arr;
return a + b;
};

const runBrainGcd = () => play(gameAsk, getCommonDivisor);
const generateDataAnswer = () => {
const [number1, number2] = pairs(min, max);
const question = `${String(number1)} ${String(number2)}`;
const divisor = getCommonDivisor(number1, number2);
return [question, String(divisor)];
};

const runBrainGcd = () => play(gameAsk, generateDataAnswer);

export default runBrainGcd;

0 comments on commit 346c1b7

Please sign in to comment.