Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
317
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 playerName = readlineSync.question('May I have your name? ');
  5.   console.log(`Hello, ${playerName}!`);
  6.   return playerName;
  7. };
  8.  
  9. const randomNumber = () => Math.floor(Math.random(1) * 100);
  10.  
  11. const genArr = (questionForGcd) => {
  12.   const arr = questionForGcd.split(' ');
  13.   arr[0] = Number(arr[0]);
  14.   arr[1] = Number(arr[1]);
  15.   return arr;
  16. };
  17.  
  18. const gcd = (genQuestion) => {
  19.   const m = genQuestion[0];
  20.   const n = genQuestion[1];
  21.   let i = m;
  22.   while (m % i !== 0 || n % i !== 0) {
  23.     i -= 1;
  24.   }
  25.   return i;
  26. };
  27.  
  28. const questionForNumber = (questionForGcd) => {
  29.   console.log(`Question: ${questionForGcd}`);
  30.   return readlineSync.question('Your answer: ');
  31. };
  32.  
  33. const gcdGame = (name, genQuestion, questionForGcd) => {
  34.   for (let correctAnswer = 0; correctAnswer < 3; questionForGcd = `${randomNumber()}${' '}${randomNumber()}`) {
  35.     const playerAnswer = Number(questionForNumber(questionForGcd));
  36.     genQuestion = genArr(questionForGcd);
  37.     if (playerAnswer === gcd(genQuestion)) {
  38.       correctAnswer += 1;
  39.       console.log('Correct!');
  40.     } else if (playerAnswer !== gcd(genQuestion)) {
  41.       console.log(`${playerAnswer} is wrong answer ;(. Correct answer was ${gcd(genQuestion)}`);
  42.       correctAnswer = 0;
  43.     }
  44.   }
  45.   console.log(`Congratulation, ${name}!`);
  46. };
  47.  
  48. const game = () => {
  49.   const questionForGcd = `${randomNumber()}${' '}${randomNumber()}`;
  50.   const genQuestion = genArr(questionForGcd);
  51.   console.log('Welcome to the Brain Games!');
  52.   const name = whatsYourName();
  53.   console.log('Find the greatest common divisor of given numbers.');
  54.   gcdGame(name, genQuestion, questionForGcd);
  55.   return genQuestion;
  56. };
  57. export default game;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement