Skip to content

Commit 53dcc78

Browse files
committed
Add new gcd game
1 parent 05b073d commit 53dcc78

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

bin/brain-gcd.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
import run from '../src/games/gcd.js';
4+
5+
run();

src/games/gcd.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import playGame from '../index.js';
2+
3+
const rules = 'Find the greatest common divisor of given numbers.';
4+
5+
const findGcd = () => {
6+
const firstNum = Math.floor(Math.random() * 150) + 1;
7+
const secondNum = Math.floor(Math.random() * 50) + 1;
8+
const minNum = Math.min(firstNum, secondNum);
9+
const question = `${firstNum} ${secondNum}`;
10+
let correctAnswer = 1;
11+
12+
for (let i = 2; i <= minNum; i += 1) {
13+
if (firstNum % i === 0 && secondNum % i === 0) {
14+
correctAnswer = i;
15+
}
16+
}
17+
18+
return [question, String(correctAnswer)];
19+
};
20+
21+
export default () => playGame(rules, findGcd);

0 commit comments

Comments
 (0)