Skip to content

Commit e63526d

Browse files
committed
Add new brain-prime game
1 parent 1f888f6 commit e63526d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

bin/brain-prime.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/prime.js';
4+
5+
run();

src/games/prime.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import playGame from '../index.js';
2+
3+
const rules = 'Answer "yes" if given number is prime. Otherwise answer "no".';
4+
5+
const askIfPrime = () => {
6+
const num = Math.floor(Math.random() * 100) + 1;
7+
let divisors = 1;
8+
let correctAnswer = '';
9+
10+
for (let i = 2; i <= num; i += 1) {
11+
if (num % i === 0) {
12+
divisors += 1;
13+
if (divisors === 3) {
14+
break;
15+
}
16+
}
17+
}
18+
correctAnswer = divisors === 2 ? 'yes' : 'no';
19+
20+
return [num, correctAnswer];
21+
};
22+
23+
export default () => playGame(rules, askIfPrime);

0 commit comments

Comments
 (0)