Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Oct 6, 2023
1 parent 5494930 commit 9119877
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 56 deletions.
53 changes: 4 additions & 49 deletions bin/brain-even.js
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 0 additions & 4 deletions bin/brain-games.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

import greetGamer from '../src/cli.js';

import isEvenNum from './brain-even.js';

greetGamer();

isEvenNum();
9 changes: 6 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 52 additions & 0 deletions src/game.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 9119877

Please sign in to comment.