YuriNem/project-lvl1-s224

View on GitHub
src/index.js

Summary

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

export const askName = (question = '') => {
  console.log('Welcome to the Brain Games!');
  if (question === '') {
    console.log();
  } else {
    console.log(`${question}\n`);
  }
  const name = readlineSync.question('May I have your name? ');
  console.log(`Hello, ${name}!`);
  return name;
};

export const flow = (game, message) => {
  const name = askName(message);
  console.log('');
  for (let i = 0; i < 3; i += 1) {
    const gameCons = game();
    console.log(`Question: ${gameCons('car')}`);
    const answer = readlineSync.question('Your answer: ');
    if (String(answer) === String(gameCons('cdr'))) {
      console.log('Correct!');
    } else {
      console.log(`'${answer}' is wrong answer ;(. Correct answer '${gameCons('cdr')}'.\nLet's try again, ${name}!`);
      return 1;
    }
  }
  console.log(`Congratulations, ${name}!`);
  return 0;
};