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 2ebe031 commit 8adceb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@ import readlineSync from 'readline-sync';

import greetGamer from './cli.js';

import getRandomInt from '../function/getRandomInt.js';

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

const calcGame = () => {
const userName = greetGamer();
console.log('What is the result of the expression?');
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 numberOne = getRandomInt(100);
const numberTwo = getRandomInt(100);
const operators = ['+', '-', '*'];
const operator = operators[getRandomInt(3)];
const expression = `${numberOne} ${operator} ${numberTwo}`;
console.log('Question: ', expression);
const answerCorrect = isRightAnswer(numberOne, numberTwo, operator);
const answerUser = readlineSync.question('Your answer: ');
if (answerUser !== answerCorrect) {
if (answerUser !== answerCorrect.toString()) {
console.log(`'${answerUser}' is wrong answer ;(. Correct answer was '${answerCorrect}'.`);
console.log("Let's try again, ", userName);
return;
Expand Down
1 change: 1 addition & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const greetGamer = () => {
console.log('Welcome to the Brain Games!');
const userName = readlineSync.question('May I have your name? ');
console.log(`Hello, ${userName}!`);
return userName;
};

export default greetGamer;

0 comments on commit 8adceb0

Please sign in to comment.