diff --git a/bin/brain-even.js b/bin/brain-even.js index a4e31d4..0f525ec 100644 --- a/bin/brain-even.js +++ b/bin/brain-even.js @@ -1,50 +1,5 @@ -import readlineSync from 'readline-sync'; -import greetGamer from '../src/cli.js'; +#!/usr/bin/env node -const isEvenNum = () => { - console.log('Answer "yes" if the number is even, otherwise answer "no".'); - let result; - do { - result = false; - for (let i = 0; i <= 3; i += 1) { - const number = Math.floor(Math.random() * 100); - console.log(`Question: ${number}`); - const answer = readlineSync.question('Your answer:'); - const name = greetGamer(); - if (number % 2 === 0) { - const answerYes = 'Correct!'; - const answerNo = `'no' is wrong answer ;(. Correct answer was 'yes'.\nLet's try again, ${name}!`; - switch (answer) { - case 'yes': - console.log(answerYes); - result = true; - break; - case 'no': - console.log(answerNo); - result = false; - break; - default: - console.log(answerNo); - result = false; - } - } else if (number % 2 !== 0) { - const answerYes = `'yes' is wrong answer ;(. Correct answer was 'no'.\nLet's try again, ${name}!`; - const answerNo = 'Correct!'; - switch (answer) { - case 'yes': - console.log(answerYes); - result = false; - break; - case 'no': - console.log(answerNo); - result = true; - break; - default: - console.log(answerNo); - result = false; - } - } - } - } while (result); -}; -export default isEvenNum; +import isEvenNum from '../src/game.js'; + +isEvenNum(); diff --git a/bin/brain-games.js b/bin/brain-games.js index 1da82ae..cdff312 100755 --- a/bin/brain-games.js +++ b/bin/brain-games.js @@ -2,8 +2,4 @@ import greetGamer from '../src/cli.js'; -import isEvenNum from './brain-even.js'; - greetGamer(); - -isEvenNum(); diff --git a/src/cli.js b/src/cli.js index 8d04b38..4a85438 100644 --- a/src/cli.js +++ b/src/cli.js @@ -1,11 +1,14 @@ import readlineSync from 'readline-sync'; -console.log('Welcome to the Brain Games!'); +export const getName = () => { + const name = readlineSync.question('May I have your name?'); + return name; +}; const greetGamer = () => { - const name = readlineSync.question('May I have your name?'); + console.log('Welcome to the Brain Games!'); + const name = getName(); console.log(`Hello, ${name}!`); - return name; }; export default greetGamer; diff --git a/src/game.js b/src/game.js new file mode 100644 index 0000000..a3abbc8 --- /dev/null +++ b/src/game.js @@ -0,0 +1,52 @@ +import readlineSync from 'readline-sync'; +import getName from './cli.js'; + +const isEvenNum = () => { + console.log('Answer "yes" if the number is even, otherwise answer "no".'); + let result; + const name = getName(); + do { + result = false; + for (let i = 0; i <= 2; i += 1) { + const number = Math.floor(Math.random() * 100); + console.log(`Question: ${number}`); + const answer = readlineSync.question('Your answer:'); + if (number % 2 === 0) { + const answerYes = 'Correct!'; + const answerNo = `'no' is wrong answer ;(. Correct answer was 'yes'.\nLet's try again, ${name}!`; + switch (answer) { + case 'yes': + console.log(answerYes); + result = true; + break; + case 'no': + console.log(answerNo); + result = false; + break; + default: + console.log(answerNo); + result = false; + } + } else if (number % 2 !== 0) { + const answerYes = `'yes' is wrong answer ;(. Correct answer was 'no'.\nLet's try again, ${name}!`; + const answerNo = 'Correct!'; + switch (answer) { + case 'yes': + console.log(answerYes); + result = false; + break; + case 'no': + console.log(answerNo); + result = true; + break; + default: + console.log(answerYes); + result = false; + } + } + } + } while (result); + return result; +}; + +export default isEvenNum;