Skip to content

Instantly share code, notes, and snippets.

@dinarname
Created March 22, 2019 07:32
Show Gist options
  • Save dinarname/2fd3a223f689d6b756f1a18d3e629e3a to your computer and use it in GitHub Desktop.
Save dinarname/2fd3a223f689d6b756f1a18d3e629e3a to your computer and use it in GitHub Desktop.
import readlineSync from 'readline-sync';
const getToKnow = () => {
const name = readlineSync.question('May I have your name? ');
console.log(`Hi, ${name}!`);
return name;
};
const evenNumberGame = () => {
console.log("Welcome to the Brain Games!");
console.log('Answer "yes" if number even otherwise answer "no".');
const name = getToKnow();
if (isPlayerWin()) {
console.log(`Congratulations, ${name}!`);
} else {
console.log(`Let's try again, ${name}!`);
}
};
const isPlayerWin = () => {
const evenNumber = (attempt) => {
if (attempt === 0) {
return true;
}
const num = getRandom(100);
const mod = num % 2;
console.log(`Question: ${num}`);
const userAnswer = readlineSync.question('Your answer: ');
if (mod === 0) {
if (userAnswer === "yes") {
console.log("Correct!");
attempt -= 1;
evenNumber(attempt);
} else {
console.log(`' ${userAnswer} ' is wrong answer ;(. Correct answer was ' yes '.`);
return false;
}
}
if (mod !== 0) {
if (userAnswer === "no") {
console.log("Correct!");
attempt -= 1;
evenNumber(attempt);
} else {
console.log(`' ${userAnswer} ' is wrong answer ;(. Correct answer was ' no '.`);
return false;
}
}
}
return evenNumber(3);
}
const getRandom = (max) => {
return Math.floor(Math.random() * max);
};
export {getToKnow, evenNumberGame};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment