dmitry-makovsky/project-lvl1-s504

View on GitHub
src/index.js

Summary

Maintainability
A
0 mins
Test Coverage
import readlineSync from 'readline-sync';

const stepsCount = 3;

export default (gameСondition, getGameData) => {
  console.log('Welcome to the Brain Games!');
  console.log(`${gameСondition}\n`);

  const name = readlineSync.question('May I have your name? ');
  console.log(`Hello, ${name}!\n`);

  const iter = (step) => {
    const { question, rightAnswer } = getGameData();
    console.log(`Question: ${question}`);
    const answer = readlineSync.question('Your answer: ');

    if (step === 1) {
      console.log(`Congratulations, ${name}`);
      return;
    }

    if (rightAnswer !== answer) {
      console.log(`'${answer}' is wrong answer ;(. Correct answer was '${rightAnswer}'`);
      console.log(`Let's try again, ${name}`);
      return;
    }

    console.log('Correct!');
    iter(step - 1);
  };
  iter(stepsCount);
};