Skip to content

Commit e79b04f

Browse files
committed
correction project
1 parent d45dc2f commit e79b04f

File tree

9 files changed

+58
-69
lines changed

9 files changed

+58
-69
lines changed

bin/brain-even.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env node
2-
import evenGame from '../src/games/cliEven.js';
2+
import evenGame from '../src/games/even.js';
33

44
evenGame();

eslint.config.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,6 @@ export default [
5252
'import/no-named-as-default-member': 'off',
5353
'no-console': 'off',
5454
'import/no-extraneous-dependencies': 'off',
55-
// 'linebreak-style': ['error', 'windows'],
5655
},
5756
},
5857
];
59-
60-
/*
61-
import globals from "globals";
62-
63-
import path from "path";
64-
import { fileURLToPath } from "url";
65-
import { FlatCompat } from "@eslint/eslintrc";
66-
import pluginJs from "@eslint/js";
67-
68-
// mimic CommonJS variables -- not needed if using CommonJS
69-
const __filename = fileURLToPath(import.meta.url);
70-
const __dirname = path.dirname(__filename);
71-
const compat = new FlatCompat({
72-
baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended
73-
});
74-
75-
export default [
76-
{languageOptions: { globals: globals.browser }},
77-
...compat.extends("airbnb"),
78-
];
79-
*/

src/games/calc.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const descriptionGame = 'What is the result of the expression?';
55

66
const actionGame = () => {
77
const sign = ['+', '-', '*'];
8-
const randomSiqn = sign[randomNumber(3)];
9-
const num1 = randomNumber();
10-
const num2 = randomNumber();
8+
const randomSign = sign[randomNumber(0, sign.length)];
9+
const num1 = randomNumber(0, 100);
10+
const num2 = randomNumber(0, 100);
1111
let correctAnswer = '';
12-
const questionMath = `${num1} ${randomSiqn} ${num2}`;
13-
switch (randomSiqn) {
12+
const questionMath = `${num1} ${randomSign} ${num2}`;
13+
switch (randomSign) {
1414
case '+':
1515
correctAnswer = num1 + num2;
1616
break;
@@ -25,8 +25,6 @@ const actionGame = () => {
2525
return [questionMath, String(correctAnswer)];
2626
};
2727

28-
const calcGame = () => {
29-
roundGame(actionGame, descriptionGame);
30-
};
28+
const calcGame = () => roundGame(actionGame, descriptionGame);
3129

3230
export default calcGame;

src/games/cliEven.js renamed to src/games/even.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import randomNumber from '../utilRandomNumber.js';
44
const descriptionGame = 'Answer "yes" if the number is even, otherwise answer "no".';
55

66
const actionGame = () => {
7-
const questNumber = randomNumber();
7+
const questNumber = randomNumber(0, 100);
88
const isEvenNum = questNumber % 2 === 0;
99
const correctAnswer = isEvenNum ? 'yes' : 'no';
1010

1111
return [questNumber, correctAnswer];
1212
};
1313

14-
const evenGame = () => {
15-
roundGame(actionGame, descriptionGame);
16-
};
14+
const evenGame = () => roundGame(actionGame, descriptionGame);
1715

1816
export default evenGame;

src/games/gcd.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ import randomNumber from '../utilRandomNumber.js';
33

44
const descriptionGame = 'Find the greatest common divisor of given numbers.';
55

6-
const actionGame = () => {
7-
let num1 = randomNumber();
8-
let num2 = randomNumber();
9-
const questionExample = `${num1} ${num2}`;
10-
while (num1 !== 0 && num2 !== 0) {
11-
if (num1 > num2) {
12-
num1 %= num2;
6+
const gcd = (num1, num2) => {
7+
let number1 = num1;
8+
let number2 = num2;
9+
while (number1 !== 0 && number2 !== 0) {
10+
if (number1 > number2) {
11+
number1 %= number2;
1312
} else {
14-
num2 %= num1;
13+
number2 %= number1;
1514
}
1615
}
17-
const correctAnswer = num1 + num2;
18-
return [questionExample, String(correctAnswer)];
16+
return number1 + number2;
1917
};
2018

21-
const gcdGame = () => {
22-
roundGame(actionGame, descriptionGame);
19+
const actionGame = () => {
20+
const num1 = randomNumber(1, 100);
21+
const num2 = randomNumber(1, 100);
22+
const questionExample = `${num1} ${num2}`;
23+
const correctAnswer = gcd(num1, num2);
24+
25+
return [questionExample, String(correctAnswer)];
2326
};
2427

28+
const gcdGame = () => roundGame(actionGame, descriptionGame);
29+
2530
export default gcdGame;

src/games/prime.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@ import randomNumber from '../utilRandomNumber.js';
33

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

6-
const actionGame = () => {
7-
const questionNum = randomNumber();
6+
const prime = (num) => {
87
let correctAnswer = 'yes';
9-
for (let i = 2; i < Math.sqrt(questionNum); i += 1) {
10-
if (questionNum % i === 0) {
8+
for (let i = 2; i < Math.sqrt(num); i += 1) {
9+
if (num % i === 0) {
1110
correctAnswer = 'no';
1211
}
1312
}
14-
if (questionNum === 0) {
13+
if (num < 2) {
1514
correctAnswer = 'no';
1615
}
17-
return [questionNum, correctAnswer];
16+
17+
return correctAnswer;
1818
};
1919

20-
const primeGame = () => {
21-
roundGame(actionGame, descriptionGame);
20+
const actionGame = () => {
21+
const questionNum = randomNumber(0, 100);
22+
const correctAnswer = prime(questionNum);
23+
return [questionNum, correctAnswer];
2224
};
2325

26+
const primeGame = () => roundGame(actionGame, descriptionGame);
27+
2428
export default primeGame;

src/games/progression.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@ import randomNumber from '../utilRandomNumber.js';
33

44
const descriptionGame = 'What number is missing in the progression?';
55

6-
const actionGame = () => {
7-
let randomNum = randomNumber();
8-
const arithmeticProgression = randomNumber(5) + 1;
9-
const progression = [randomNum];
10-
for (let i = 0; i < 10; i += 1) {
11-
randomNum += arithmeticProgression;
12-
progression.push(randomNum);
6+
const generateProgression = (firstNumber, step, length) => {
7+
let startNum = firstNumber;
8+
const progression = [startNum];
9+
for (let i = 0; i < length; i += 1) {
10+
startNum += step;
11+
progression.push(startNum);
1312
}
14-
const hiddenNum = randomNumber(10);
13+
return progression;
14+
};
15+
16+
const actionGame = () => {
17+
const firstNumber = randomNumber(0, 100);
18+
const step = randomNumber(0, 5) + 1;
19+
const length = randomNumber(10, 20);
20+
const hiddenNum = randomNumber(10, length);
21+
const progression = generateProgression(firstNumber, step, length);
1522
const correctAnswer = progression[hiddenNum];
1623
progression[hiddenNum] = '..';
1724
const question = progression.join(' ');
1825
return [question, String(correctAnswer)];
1926
};
2027

21-
const progressionGame = () => {
22-
roundGame(actionGame, descriptionGame);
23-
};
28+
const progressionGame = () => roundGame(actionGame, descriptionGame);
29+
2430
export default progressionGame;

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import readlineSync from 'readline-sync';
22

3-
const maxWin = 3;
3+
const MaxWin = 3;
44

55
const roundGame = (game, description) => {
66
console.log('Welcome to the Brain Games!');
77
const userName = readlineSync.question('May I have your name? ');
88
console.log(`Hello, ${userName}!`);
99
console.log(description);
10-
for (let winCount = 0; winCount < maxWin; winCount += 1) {
10+
for (let winCount = 0; winCount < MaxWin; winCount += 1) {
1111
const [question, correctAnswer] = game();
1212
console.log(`Question: ${question}`);
1313
const answer = readlineSync.question('Your answer: ');

src/utilRandomNumber.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const randomNumber = (maxRandomNumber = 100) => Math.floor(Math.random() * maxRandomNumber);
1+
const randomNumber = (min, max) => Math.floor(Math.random() * (max - min) + min);
22

33
export default randomNumber;

0 commit comments

Comments
 (0)