Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import readlineSync from 'readline-sync';
- const whatsYourName = () => {
- const actual = readlineSync.question('May I have your name? ');
- console.log(`Hello, ${actual}!`);
- const playerName = actual;
- return playerName;
- };
- const randomNumber = () => Math.floor(Math.random(1) * 100);
- const mathSigns = ['+', '-', '*'];
- const genMathSign = () => Math.floor(Math.random(1) * 3);
- const randomMathSign = () => mathSigns[genMathSign()];
- const trueAnswer = () => {
- let result = '';
- if (randomMathSign() === '+') {
- result = randomNumber() + randomNumber();
- }
- if (randomMathSign() === '-') {
- result = randomNumber() - randomNumber();
- }
- if (randomMathSign() === '*') {
- result = randomNumber() * randomNumber();
- }
- return result;
- };
- const questionForCalc = `${randomNumber()} ${randomMathSign()} ${randomNumber()}`;
- const questionForNumber = () => {
- console.log(`Question: ${questionForCalc}`);
- return readlineSync.question('Your answer: ');
- };
- const calcGame = (name, playerAnswer, result) => {
- for (let correctAnswer = 0; correctAnswer < 3; questionForNumber()) {
- trueAnswer();
- if (playerAnswer === result) {
- correctAnswer += 1;
- console.log('Correct!');
- } else if (playerAnswer !== result) {
- console.log(`${playerAnswer} is wrong answer ;(. Correct answer was ${result}`);
- console.log(`Let's try again, ${name}`);
- correctAnswer = 0;
- }
- }
- console.log(`Congratulation, ${name}!`);
- };
- const game = () => {
- const genRandomNum = randomNumber();
- console.log('Welcome to the Brain Games!');
- const name = whatsYourName();
- console.log('What is the result of the expression?');
- const playerAnswer = questionForNumber();
- calcGame(name, playerAnswer);
- return genRandomNum;
- };
- export default game;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement