Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Oct 17, 2023
1 parent 080067f commit b2bbd29
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 111 deletions.
4 changes: 2 additions & 2 deletions bin/brain-even.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

import startGame from '../games/even.js';
import isItEven from '../games/even.js';

startGame();
isItEven();
4 changes: 2 additions & 2 deletions bin/brain-gcd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

import maxGeneralDivider from '../games/gcd.js';
import isMaxGeneralDivider from '../games/gcd.js';

maxGeneralDivider();
isMaxGeneralDivider();
4 changes: 2 additions & 2 deletions bin/brain-progression.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

import progression from '../games/progression.js';
import getProgression from '../games/progression.js';

progression();
getProgression();
34 changes: 12 additions & 22 deletions games/calc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import readlineSync from 'readline-sync';

import greetGamer from '../src/cli.js';
import game from '../index.js';

import getRandomInt from '../function/getRandomInt.js';

Expand All @@ -22,25 +20,17 @@ const isRightAnswer = (numberOne, numberTwo, operator) => {
return result;
};

const playCalc = () => {
const numberOne = getRandomInt(100);
const numberTwo = getRandomInt(100);
const operators = ['+', '-', '*'];
const operator = operators[getRandomInt(3)];
const question = `${numberOne} ${operator} ${numberTwo}`;
const answerCorrect = isRightAnswer(numberOne, numberTwo, operator);
return [question, answerCorrect];
};
const calcGame = () => {
const userName = greetGamer();
console.log('What is the result of the expression?');
for (let i = 0; i < 3; i += 1) {
const numberOne = getRandomInt(100);
const numberTwo = getRandomInt(100);
const operators = ['+', '-', '*'];
const operator = operators[getRandomInt(3)];
const expression = `${numberOne} ${operator} ${numberTwo}`;
console.log('Question: ', expression);
const answerCorrect = isRightAnswer(numberOne, numberTwo, operator);
const answerUser = readlineSync.question('Your answer: ');
if (answerUser !== answerCorrect.toString()) {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`);
console.log("Let's try again, ", userName);
return;
}
console.log('Correct!');
}
console.log('Congratulations,', userName, '!');
const condition = ('What is the result of the expression?');
game(playCalc, condition);
};
export default calcGame;
30 changes: 10 additions & 20 deletions games/even.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import readlineSync from 'readline-sync';
import game from '../index.js';

import getRandomInt from '../function/getRandomInt.js';

const isEvenNum = (number) => number % 2 === 0;

const startGame = () => {
console.log('Welcome to the Brain Games!');
const userName = readlineSync.question('May I have your name? ');
console.log(`Hello, ${userName}!`);
console.log('Answer "yes" if the number is even, otherwise answer "no".');
for (let i = 0; i < 3; i += 1) {
const number = getRandomInt(100);
console.log('Question: ', number);
const answerCorrect = isEvenNum(number) ? 'yes' : 'no';
const answerUser = readlineSync.question('Your answer: ');
if (answerUser !== answerCorrect) {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`);
console.log("Let's try again, ", userName);
return;
}
console.log('Correct!');
}
console.log('Congratulations,', userName, '!');
const playEven = () => {
const question = String(getRandomInt(100));
const answerCorrect = isEvenNum(question) ? 'yes' : 'no';
return [question, answerCorrect];
};
export default startGame;
const isItEven = () => {
const condition = ('Answer "yes" if the number is even, otherwise answer "no".');
game(playEven, condition);
};
export default isItEven;
34 changes: 12 additions & 22 deletions games/gcd.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import readlineSync from 'readline-sync';

import greetGamer from '../src/cli.js';
import game from '../index.js';

import getRandomInt from '../function/getRandomInt.js';

Expand All @@ -19,23 +17,15 @@ const getMaxDevidor = (numberOne, numberTwo) => {
}
} return maxDevidor;
};
const maxGeneralDivider = () => {
const userName = greetGamer();
console.log('Find the greatest common divisor of given numbers.');
for (let i = 0; i < 3; i += 1) {
const numberOne = getRandomInt(100);
const numberTwo = getRandomInt(100);
const expression = `${numberOne} ${numberTwo}`;
console.log('Question: ', expression);
const answerCorrect = getMaxDevidor(numberOne, numberTwo);
const answerUser = readlineSync.question('Your answer: ');
if (answerUser !== answerCorrect.toString()) {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`);
console.log("Let's try again, ", userName);
return;
}
console.log('Correct!');
}
console.log('Congratulations,', userName, '!');
const playMaxGeneralDivider = () => {
const numberOne = getRandomInt(100);
const numberTwo = getRandomInt(100);
const question = `${numberOne} ${numberTwo}`;
const answerCorrect = getMaxDevidor(numberOne, numberTwo);
return [question, answerCorrect];
};
const isMaxGeneralDivider = () => {
const condition = ('Find the greatest common divisor of given numbers.');
game(playMaxGeneralDivider, condition);
};
export default maxGeneralDivider;
export default isMaxGeneralDivider;
43 changes: 20 additions & 23 deletions games/prime.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import readlineSync from 'readline-sync';

import greetGamer from '../src/cli.js';

import getRandomInt from '../function/getRandomInt.js';

const isNumPrime = (num) => {
if (num === 1) {
return 'no';
import game from '../index.js';

const isNumPrime = (value) => {
const num = Number(value);
if (num === 1 || num === 0) {
return false;
}
for (let i = 2; i < value; i += 1) {
if (num % i === 0) {
return false;
}
}
return (num % 2 === 0 || num % 3 === 0) ? 'no' : 'yes';
return true;
};

const playPrime = () => {
const question = String(getRandomInt(100));
const answerCorrect = isNumPrime(question) ? 'yes' : 'no';
return [question, answerCorrect];
};

const isItPrime = () => {
const userName = greetGamer();
console.log('Answer "yes" if given number is prime. Otherwise answer "no".');
for (let i = 0; i < 3; i += 1) {
const number = getRandomInt(100);
console.log('Question: ', number);
const answerCorrect = isNumPrime(number);
const answerUser = readlineSync.question('Your answer: ');
if (answerUser !== answerCorrect) {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`);
console.log("Let's try again, ", userName);
return;
}
console.log('Correct!');
}
console.log('Congratulations,', userName, '!');
const condition = ('Answer "yes" if given number is prime. Otherwise answer "no".');
game(playPrime, condition);
};
export default isItPrime;
27 changes: 9 additions & 18 deletions games/progression.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import readlineSync from 'readline-sync';

import greetGamer from '../src/cli.js';
import game from '../index.js';

import getRandomInt, { getRandomArbitrary } from '../function/getRandomInt.js';

Expand All @@ -22,19 +20,12 @@ const getArray = () => {
};

const progression = () => {
const userName = greetGamer();
console.log('What number is missing in the progression?');
for (let i = 0; i < 3; i += 1) {
const [expression, answer] = getArray();
console.log('Question: ', expression.join(' '));
const answerUser = readlineSync.question('Your answer: ');
if (answerUser !== answer.toString()) {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answer}'.`);
console.log("Let's try again, ", userName);
return;
}
console.log('Correct!');
}
console.log('Congratulations,', userName, '!');
const [expression, answerCorrect] = getArray();
const question = expression.join(' ');
return [question, answerCorrect];
};
const getProgression = () => {
const condition = ('What number is missing in the progression?');
game(progression, condition);
};
export default progression;
export default getProgression;
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import readlineSync from 'readline-sync';

import greetGamer from './src/cli.js';

const game = (partGame, condition) => {
const userName = greetGamer();
console.log(condition);
for (let i = 0; i < 3; i += 1) {
const [question, answerCorrect] = partGame();
console.log('Question: ', question);
const answerUser = readlineSync.question('Your answer: ');
if (answerUser === String(answerCorrect)) {
console.log('Correct!');
} else {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`);
console.log("Let's try again, ", userName);
return;
}
}
console.log('Congratulations,', userName, '!');
};

export default game;

0 comments on commit b2bbd29

Please sign in to comment.