Skip to content

Commit 6b2f1d1

Browse files
committed
fix structure of brain-even.js and index.js
1 parent d36fa6a commit 6b2f1d1

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

bin/brain-even.js

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,5 @@
11
#!/usr/bin/env node
2-
import readlineSync from 'readline-sync';
3-
import { greeting } from '../index.js';
2+
import { playGame } from '../index.js';
43

5-
// приветствие
6-
greeting ();
7-
8-
// поясняем смысл
9-
console.log('Answer "yes" if the number is even, otherwise answer "no".');
10-
11-
// цикл подсчета
12-
let correctAnswersCount = 0;
13-
14-
while (correctAnswersCount < 3) {
15-
// получаем число от 1 до 100
16-
const getRandomNumber = Math.floor(Math.random() * 100) + 1;
17-
// выводим число
18-
console.log(`Question: ${getRandomNumber}`);
19-
// получаем ответ пользователя
20-
const getAnswer = readlineSync.question('Your answer: ');
21-
22-
// проверяем четность
23-
const checkEvenness = (number) => (number % 2 === 0 ? 'yes' : 'no'); // Возвращаем 'yes', если четное, иначе 'no'
24-
25-
// Получаем правильный ответ
26-
const correctAnswer = checkEvenness(getRandomNumber);
27-
// сравниваем ответы
28-
if (getAnswer === correctAnswer) {
29-
console.log('Correct!');
30-
correctAnswersCount++;
31-
// Проверяем, достигли ли мы 3 правильных ответа
32-
if (correctAnswersCount === 3) {
33-
console.log(`Congratulations, ${userName}!`);
34-
// закрываем цикл
35-
break;
36-
}
37-
} else {
38-
console.log(`Answer "${getAnswer}" if the number is even, otherwise answer "${correctAnswer}".`);
39-
// закрываем цикл
40-
break;
41-
}
42-
}
4+
// Запускаем игру
5+
playGame();

index.js

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
1-
import readlineSync from 'readline-sync';
2-
3-
export const greeting = () => {
4-
console.log('Welcome to the Brain Games!');
5-
const userName = readlineSync.question('May I have your name? ');
6-
console.log(`Hello, ${userName}!`);
7-
};
1+
import readlineSync from 'readline-sync';
2+
3+
// Функция приветствия
4+
export const greeting = () => {
5+
console.log('Welcome to the Brain Games!');
6+
const userName = readlineSync.question('May I have your name? ');
7+
console.log(`Hello, ${userName}!`);
8+
return userName;
9+
};
10+
11+
// Функция для генерации случайного числа от 1 до 100
12+
export const getRandomNumber = () => {
13+
return Math.floor(Math.random() * 100) + 1;
14+
};
15+
16+
// Функция для проверки четности
17+
export const checkEvenness = (number) => {
18+
// Возвращаем 'yes', если четное, иначе 'no'
19+
return number % 2 === 0 ? 'yes' : 'no';
20+
};
21+
22+
// Функция подсчета ответов
23+
const handleAnswer = (userAnswer, correctAnswer, userName, correctAnswersCount) => {
24+
// Сравниваем ответы
25+
if (userAnswer === correctAnswer) {
26+
console.log('Correct!');
27+
correctAnswersCount += 1;
28+
// Проверяем, достигли ли мы 3 правильных ответа
29+
if (correctAnswersCount === 3) {
30+
console.log(`Congratulations, ${userName}!`);
31+
// Завершение игры при достижении 3
32+
return { correctAnswersCount, finished: true };
33+
}
34+
} else {
35+
console.log(`Answer "${userAnswer}" if the number is even, otherwise answer "${correctAnswer}".`);
36+
// Завершение игры при неправильном ответе
37+
return { correctAnswersCount, finished: true };
38+
}
39+
// Игра продолжается
40+
return { correctAnswersCount, finished: false };
41+
};
42+
43+
// brain-even
44+
export const playGame = () => {
45+
const userName = greeting();
46+
let correctAnswersCount = 0;
47+
// Поясняем смысл
48+
console.log('Answer "yes" if the number is even, otherwise answer "no".');
49+
// Цикл игры
50+
while (correctAnswersCount < 3) {
51+
// Получаем число
52+
const number = getRandomNumber();
53+
console.log(`Question: ${number}`);
54+
// Получаем ответ пользователя
55+
const userAnswer = readlineSync.question('Your answer: ');
56+
// Проверяем четность
57+
const correctAnswer = checkEvenness(number);
58+
// Сверяем результаты
59+
const result = handleAnswer(userAnswer, correctAnswer, userName, correctAnswersCount);
60+
correctAnswersCount = result.correctAnswersCount;
61+
// Закрываем цикл
62+
if (result.finished) {
63+
break;
64+
}
65+
}
66+
};

0 commit comments

Comments
 (0)