brain-game
Version:
56 lines (42 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _readlineSync = require('readline-sync');
var _readlineSync2 = _interopRequireDefault(_readlineSync);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const showQuestion = question => {
console.log(`Question: ${ question }`);
};
const getAnswer = () => _readlineSync2.default.question('Your answer: ');
const showGreeting = () => console.log('Welcome to the Brain Games!');
const getName = () => _readlineSync2.default.question('May I have your name? ');
const showHello = name => console.log(`Hello, ${ name }!\n`);
const check = (correctChoise, userChoise) => correctChoise === userChoise ? 'Correct!' : `${ userChoise } is wrong answer ;(. Correct answer was ${ correctChoise }.`;
const step = (genData, getCorrectResult, toString) => {
const data = genData();
const question = toString(data);
showQuestion(question);
const userChoise = getAnswer();
const correctChoise = `${ getCorrectResult(data) }`;
return check(correctChoise, userChoise);
};
const iter = (acc, name, maxAttempts, genData, getCorrectResult, toString) => {
if (acc === maxAttempts) {
return console.log(`Congratulations, ${ name }!`);
}
const result = step(genData, getCorrectResult, toString);
console.log(result);
if (result === 'Correct!') {
return iter(acc + 1, name, maxAttempts, genData, getCorrectResult, toString);
}
return `Let's try again, ${ name }!`;
};
const playFullGame = (description, genData, getCorrectResult, toString, maxAttempts = 3) => {
showGreeting();
console.log(description);
const name = getName();
showHello(name);
return iter(0, name, maxAttempts, genData, getCorrectResult, toString);
};
exports.default = playFullGame;