Akitory4/project-lvl1-s92

View on GitHub
src/brain-games.js

Summary

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

const COUNT_QUESTIONS = 3;

const gameplay = (game) => {
  for (let i = 0; i < COUNT_QUESTIONS; i += 1) {
    const gameData = game();
    const userAnswer = readlineSync.question(`${gameData.question} `);
    const correctAnswer = gameData.correct;
    if (userAnswer === correctAnswer) {
      console.log('Correct!');
    } else {
      console.log(`'${userAnswer}' is wrong answer ;(. Correct answer was '${correctAnswer}'.`);
      return false;
    }
  }
  return true;
};

export const newGame = (rules, game) => {
  console.log('Welcome to the Brain Games!');
  console.log(rules);
  const userName = readlineSync.question('May I have your name?');
  console.log(`Hello, ${userName}!`);
  if (gameplay(game)) {
    console.log(`Congratulations, ${userName}!`);
  } else {
    console.log(`Let's try again, ${userName}!`);
  }
};

export default newGame;