Skip to content

Commit 664ca28

Browse files
committed
Update architecture & bug fixes
1 parent eedeec7 commit 664ca28

File tree

6 files changed

+77
-46
lines changed

6 files changed

+77
-46
lines changed

src/games/calc.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
import playGame from '../index.js';
1+
import playGame, { getRandomNum } from '../index.js';
22

33
const rules = 'What is the result of the expression?';
44

5-
const calc = () => {
6-
const firstNum = Math.floor(Math.random() * 50) + 1;
7-
const secondNum = Math.floor(Math.random() * 25) + 1;
8-
const operations = ['+', '-', '*'];
9-
const operation = operations[Math.floor(Math.random() * 3)];
10-
const expression = `${firstNum} ${operation} ${secondNum}`;
11-
let correctAnswer = 0;
5+
const calc = (operation, a, b) => {
6+
let result = 0;
127

138
switch (operation) {
149
case '+':
15-
correctAnswer = firstNum + secondNum;
10+
result = a + b;
1611
break;
1712
case '-':
18-
correctAnswer = firstNum - secondNum;
13+
result = a - b;
1914
break;
2015
case '*':
21-
correctAnswer = firstNum * secondNum;
16+
result = a * b;
2217
break;
2318
default:
2419
break;
2520
}
2621

22+
return result;
23+
};
24+
25+
const getGameData = () => {
26+
const firstNum = getRandomNum(50) + 1;
27+
const secondNum = getRandomNum(25) + 1;
28+
const operations = ['+', '-', '*'];
29+
const operation = operations[getRandomNum(3)];
30+
const expression = `${firstNum} ${operation} ${secondNum}`;
31+
const correctAnswer = calc(operation, firstNum, secondNum);
32+
2733
return [expression, String(correctAnswer)];
2834
};
2935

30-
export default () => playGame(rules, calc);
36+
export default () => playGame(rules, getGameData);

src/games/even.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import playGame from '../index.js';
1+
import playGame, { getRandomNum } from '../index.js';
22

33
const rules = 'Answer "yes" if the number is even, otherwise answer "no".';
44

5-
const askIsEven = () => {
6-
const evenNum = Math.floor(Math.random() * 100) + 1;
7-
const correctAnswer = evenNum % 2 === 0 ? 'yes' : 'no';
8-
return [evenNum, correctAnswer];
5+
const isEven = (num) => num % 2 === 0;
6+
7+
const getGameData = () => {
8+
const num = getRandomNum(100) + 1;
9+
const correctAnswer = isEven(num) ? 'yes' : 'no';
10+
11+
return [num, correctAnswer];
912
};
1013

11-
export default () => playGame(rules, askIsEven);
14+
export default () => playGame(rules, getGameData);

src/games/gcd.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import playGame from '../index.js';
1+
import playGame, { getRandomNum } from '../index.js';
22

33
const rules = 'Find the greatest common divisor of given numbers.';
44

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;
5+
const getGcd = (a, b) => {
6+
const min = Math.min(a, b);
7+
let result = 1;
118

12-
for (let i = 2; i <= minNum; i += 1) {
13-
if (firstNum % i === 0 && secondNum % i === 0) {
14-
correctAnswer = i;
9+
for (let i = 2; i <= min; i += 1) {
10+
if (a % i === 0 && b % i === 0) {
11+
result = i;
1512
}
1613
}
1714

15+
return result;
16+
};
17+
18+
const getGameData = () => {
19+
const firstNum = getRandomNum(150) + 1;
20+
const secondNum = getRandomNum(50) + 1;
21+
const question = `${firstNum} ${secondNum}`;
22+
const correctAnswer = getGcd(firstNum, secondNum);
23+
1824
return [question, String(correctAnswer)];
1925
};
2026

21-
export default () => playGame(rules, findGcd);
27+
export default () => playGame(rules, getGameData);

src/games/prime.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import playGame from '../index.js';
1+
import playGame, { getRandomNum } from '../index.js';
22

33
const rules = 'Answer "yes" if given number is prime. Otherwise answer "no".';
44

5-
const askIfPrime = () => {
6-
const num = Math.floor(Math.random() * 100) + 1;
5+
const isPrime = (num) => {
76
let divisors = 1;
8-
let correctAnswer = '';
7+
let result = '';
98

109
for (let i = 2; i <= num; i += 1) {
1110
if (num % i === 0) {
@@ -15,9 +14,16 @@ const askIfPrime = () => {
1514
}
1615
}
1716
}
18-
correctAnswer = divisors === 2 ? 'yes' : 'no';
17+
result = divisors === 2 ? 'yes' : 'no';
18+
19+
return result;
20+
};
21+
22+
const getGameData = () => {
23+
const num = getRandomNum(100) + 1;
24+
const correctAnswer = isPrime(num);
1925

2026
return [num, correctAnswer];
2127
};
2228

23-
export default () => playGame(rules, askIfPrime);
29+
export default () => playGame(rules, getGameData);

src/games/progression.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
import playGame from '../index.js';
1+
import playGame, { getRandomNum } from '../index.js';
22

33
const rules = 'What number is missing in the progression?';
44

5-
const findNumberInProgression = () => {
6-
const increasedBy = Math.floor(Math.random() * 10) + 1;
7-
let progressionNumber = Math.floor(Math.random() * 50);
5+
const getProgression = () => {
6+
const increasedBy = getRandomNum(10) + 1;
7+
let progressionNumber = getRandomNum(50);
88
const progression = [progressionNumber];
99

1010
for (let i = 1; i < 10; i += 1) {
1111
progressionNumber += increasedBy;
1212
progression.push(progressionNumber);
1313
}
1414

15-
const position = Math.floor(Math.random() * 10);
15+
return progression;
16+
};
17+
18+
const getGameData = () => {
19+
const progression = getProgression();
20+
const position = getRandomNum(10);
1621
const correctAnswer = progression[position];
1722
progression[position] = '..';
1823

1924
return [progression.join(' '), String(correctAnswer)];
2025
};
2126

22-
export default () => playGame(rules, findNumberInProgression);
27+
export default () => playGame(rules, getGameData);

src/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import readlineSync from 'readline-sync';
2-
import getName from './cli.js';
32

4-
const playGame = (rules, game) => {
5-
const name = getName();
3+
export const getRandomNum = (x) => {
4+
const num = Math.floor(Math.random() * x);
5+
6+
return num;
7+
};
8+
9+
export default (rules, game) => {
10+
console.log('Welcome to the Brain Games!');
11+
const name = readlineSync.question('May I have your name? ');
12+
console.log(`Hello, ${name}!`);
613
console.log(rules);
714

815
for (let gameRound = 1; gameRound <= 3; gameRound += 1) {
@@ -22,5 +29,3 @@ const playGame = (rules, game) => {
2229
}
2330
}
2431
};
25-
26-
export default playGame;

0 commit comments

Comments
 (0)