Skip to content

Commit 04cdb45

Browse files
committed
update index.js games/brain-progression.js
1 parent 16882a4 commit 04cdb45

File tree

2 files changed

+48
-48
lines changed

2 files changed

+48
-48
lines changed

games/brain-progression.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
import readlineSync from 'readline-sync';
2-
import { greeting, generateRandomArray, replaceValueInArray, handleAnswer } from '../index.js';
3-
// brain-even
4-
export const playGameProgression = () => {
5-
//Приветствуем
6-
const userName = greeting();
7-
// Поясняем смысл
8-
console.log('Find the greatest common divisor of given numbers.');
9-
// Цикл игры
10-
let correctAnswersCount = 0;
11-
while (correctAnswersCount < 3) {
12-
// Получаем массив
13-
const array = generateRandomArray();
14-
// Определяем скрываемое число
15-
const randomIndex = Math.floor(Math.random() * array.length);
16-
const correctAnswer = array[randomIndex];
17-
const arrayHidden = replaceValueInArray(array, correctAnswer, "..")
18-
console.log(`Question: ${arrayHidden}`);
19-
// Получаем ответ пользователя
20-
const userAnswer = readlineSync.question('Your answer: ');
21-
// Проверяем четность
22-
const correctAnswerString = String(correctAnswer);
23-
// Сверяем результаты
24-
const incorrectMessage = `'${userAnswer}' is wrong answer ;(. Correct answer was '${correctAnswer}'.\nLet's try again, ${userName}!`;
25-
const result = handleAnswer(userAnswer, correctAnswerString, userName, correctAnswersCount, incorrectMessage);
26-
correctAnswersCount = result.correctAnswersCount;
27-
// Закрываем цикл
28-
if (result.finished) {
29-
break;
30-
}
31-
}
32-
};
1+
import readlineSync from 'readline-sync';
2+
import {
3+
greeting, generateRandomArray, replaceValueInArray, handleAnswer,
4+
} from '../index.js';
5+
// brain-even
6+
export const playGameProgression = () => {
7+
// Приветствуем
8+
const userName = greeting();
9+
// Поясняем смысл
10+
console.log('Find the greatest common divisor of given numbers.');
11+
// Цикл игры
12+
let correctAnswersCount = 0;
13+
while (correctAnswersCount < 3) {
14+
// Получаем массив
15+
const array = generateRandomArray();
16+
// Определяем скрываемое число
17+
const randomIndex = Math.floor(Math.random() * array.length);
18+
const correctAnswer = array[randomIndex];
19+
const arrayHidden = replaceValueInArray(array, correctAnswer, '..');
20+
console.log(`Question: ${arrayHidden}`);
21+
// Получаем ответ пользователя
22+
const userAnswer = readlineSync.question('Your answer: ');
23+
// Проверяем четность
24+
const correctAnswerString = String(correctAnswer);
25+
// Сверяем результаты
26+
const incorrectMessage = `'${userAnswer}' is wrong answer ;(. Correct answer was '${correctAnswer}'.\nLet's try again, ${userName}!`;
27+
const result = handleAnswer(userAnswer, correctAnswerString, userName, correctAnswersCount, incorrectMessage);
28+
correctAnswersCount = result.correctAnswersCount;
29+
// Закрываем цикл
30+
if (result.finished) {
31+
break;
32+
}
33+
}
34+
};

index.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const greeting = () => {
1212
export const getRandomNumber = () => Math.floor(Math.random() * 100) + 1;
1313

1414
// Функция для проверки четности, 'yes' если четное, иначе 'no'
15-
export const checkEvenness = (number) => number % 2 === 0 ? 'yes' : 'no';
15+
export const checkEvenness = (number) => (number % 2 === 0 ? 'yes' : 'no');
1616

1717
// Функция подсчета ответов
1818
export const handleAnswer = (userAnswer, correctAnswer, userName, correctAnswersCount, incorrectMessage) => {
@@ -21,14 +21,14 @@ export const handleAnswer = (userAnswer, correctAnswer, userName, correctAnswers
2121
console.log('Correct!');
2222
correctAnswersCount += 1;
2323
if (correctAnswersCount === 3) {
24-
// Завершение игры при достижении 3
24+
// Завершение игры при достижении 3
2525
console.log(`Congratulations, ${userName}!`);
2626
return { correctAnswersCount, finished: true };
2727
}
2828
} else {
29-
// Завершение игры при неправильном ответе
30-
console.log(incorrectMessage);
31-
return { correctAnswersCount, finished: true };
29+
// Завершение игры при неправильном ответе
30+
console.log(incorrectMessage);
31+
return { correctAnswersCount, finished: true };
3232
}
3333
// Игра продолжается
3434
return { correctAnswersCount, finished: false };
@@ -42,9 +42,9 @@ export const getRandomOperator = () => {
4242
return randomOperator;
4343
};
4444

45-
// Функция вычисления результата математической операции
45+
// Функция вычисления результата математической операции
4646
export const getResult = (num1, operator, num2) => {
47-
//Определяем значение оператора
47+
// Определяем значение оператора
4848
switch (operator) {
4949
case '+':
5050
return num1 + num2;
@@ -57,11 +57,11 @@ export const getResult = (num1, operator, num2) => {
5757

5858
// Функция вычисления НОД
5959
export const gcd = (num1, num2) => {
60-
//Алгоритм Евклида
60+
// Алгоритм Евклида
6161
while (num2 !== 0) {
62-
let temp = num2;
63-
num2 = num1 % num2;
64-
num1 = temp;
62+
const temp = num2;
63+
num2 = num1 % num2;
64+
num1 = temp;
6565
}
6666
return num1;
6767
};
@@ -74,13 +74,11 @@ export const generateRandomArray = () => {
7474
// Создаем массив
7575
const array = [];
7676
// Заполняем массив 10 числами
77-
for (let i = 0; i < 10; i++) {
78-
array.push(start + (i * step));
77+
for (let i = 0; i < 10; i += 1) {
78+
array.push(start + (i * step));
7979
}
8080
return array;
8181
};
8282

8383
// Функция для изменнения значения в массиве
84-
export const replaceValueInArray = (array, oldValue, newValue) => {
85-
return array.map(item => item === oldValue ? newValue : item)
86-
};
84+
export const replaceValueInArray = (array, oldValue, newValue) => array.map((item) => (item === oldValue ? newValue : item));

0 commit comments

Comments
 (0)