Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import readlineSync from 'readline-sync';
  2.  
  3. const whatsYourName = () => {
  4.   const actual = readlineSync.question('May I have your name? ');
  5.   console.log(`Hello, ${actual}!`);
  6.   const playerName = actual;
  7.   return playerName;
  8. };
  9.  
  10. const randomNumber = () => Math.floor(Math.random(1) * 100);
  11.  
  12. const mathSigns = ['+', '-', '*'];
  13.  
  14. const genMathSign = () => Math.floor(Math.random(1) * 3);
  15.  
  16. const randomMathSign = () => mathSigns[genMathSign()];
  17.  
  18. const trueAnswer = () => {
  19.   let result = '';
  20.   if (randomMathSign() === '+') {
  21.     result = randomNumber() + randomNumber();
  22.   }
  23.   if (randomMathSign() === '-') {
  24.     result = randomNumber() - randomNumber();
  25.   }
  26.   if (randomMathSign() === '*') {
  27.     result = randomNumber() * randomNumber();
  28.   }
  29.   return result;
  30. };
  31.  
  32. const questionForCalc = `${randomNumber()} ${randomMathSign()} ${randomNumber()}`;
  33.  
  34. const questionForNumber = () => {
  35.   console.log(`Question: ${questionForCalc}`);
  36.   return readlineSync.question('Your answer: ');
  37. };
  38.  
  39. const calcGame = (name, playerAnswer, result) => {
  40.   for (let correctAnswer = 0; correctAnswer < 3; questionForNumber()) {
  41.     trueAnswer();
  42.     if (playerAnswer === result) {
  43.       correctAnswer += 1;
  44.       console.log('Correct!');
  45.     } else if (playerAnswer !== result) {
  46.       console.log(`${playerAnswer} is wrong answer ;(. Correct answer was ${result}`);
  47.       console.log(`Let's try again, ${name}`);
  48.      correctAnswer = 0;
  49.    }
  50.  }
  51.  console.log(`Congratulation, ${name}!`);
  52. };
  53.  
  54. const game = () => {
  55.  const genRandomNum = randomNumber();
  56.  console.log('Welcome to the Brain Games!');
  57.  const name = whatsYourName();
  58.  console.log('What is the result of the expression?');
  59.  const playerAnswer = questionForNumber();
  60.  calcGame(name, playerAnswer);
  61.  return genRandomNum;
  62. };
  63. export default game;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement