src/index.js
import readlineSync from 'readline-sync';
import { car, cdr } from 'hexlet-pairs';
const rounds = 3;
const playRounds = (getQuestionAndAnswer) => {
for (let i = 1; i <= rounds; i += 1) {
const questionAndAnswer = getQuestionAndAnswer();
const question = car(questionAndAnswer);
console.log(`Question: ${question}`);
const trueAnswer = String(cdr(questionAndAnswer));
const answer = readlineSync.question('Your answer: ');
if (answer !== trueAnswer) {
console.log(`${answer} is wrong answer ;(. Correct answer was ${trueAnswer}.`);
return false;
}
console.log('Correct');
}
return true;
};
const game = (description, getQuestionAndAnswer) => {
console.log('Welcome to the Brain Games!');
console.log(`${description}\n`);
const username = readlineSync.question('May I have your name? ');
console.log(`Hello, ${username}!\n`);
const isWin = playRounds(getQuestionAndAnswer);
if (isWin) {
console.log(`Congratulations, ${username}!`);
} else {
console.log(`Let's try again, ${username}!`);
}
};
export default game;