Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Oct 11, 2023
1 parent f0bf69e commit 2ebe031
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ lint:
npx eslint .

brain-even:
node bin/brain-even.js
node bin/brain-even.js

brain-calc:
node bin/brain-calc.js

5 changes: 5 additions & 0 deletions bin/brain-calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import calcGame from '../src/calc.js';

calcGame();
38 changes: 38 additions & 0 deletions src/calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import readlineSync from 'readline-sync';

import greetGamer from './cli.js';

const isRightAnswer = (numberOne, numberTwo, operator) => {
switch (operator) {
case '+':
return numberOne + numberTwo;
case '-':
return numberOne - numberTwo;
case '*':
return numberOne * numberTwo;
default:
break;
}
};

const calcGame = () => {
for (let i = 0; i < 3; i += 1) {
const userName = greetGamer();
console.log('What is the result of the expression?');
const numberOne = Math.floor(Math.random() * 100);
const numberTwo = Math.floor(Math.random() * 100);
const operator = '+' || '-' || '*';
const expression = `${numberOne} ${operator} ${numberTwo}`;
console.log('Question: ', expression);
const answerCorrect = isRightAnswer(numberOne, numberTwo, operator);
const answerUser = readlineSync.question('Your answer: ');
if (answerUser !== answerCorrect) {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`);
console.log("Let's try again, ", userName);
return;
}
console.log('Correct!');
}
console.log('Congratulations,', userName, '!');
};
export default calcGame;

0 comments on commit 2ebe031

Please sign in to comment.