diff --git a/Makefile b/Makefile index 268d006..144b51d 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,8 @@ lint: npx eslint . brain-even: - node bin/brain-even.js \ No newline at end of file + node bin/brain-even.js + +brain-calc: + node bin/brain-calc.js + \ No newline at end of file diff --git a/bin/brain-calc.js b/bin/brain-calc.js new file mode 100644 index 0000000..6c02ff3 --- /dev/null +++ b/bin/brain-calc.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +import calcGame from '../src/calc.js'; + +calcGame(); diff --git a/src/calc.js b/src/calc.js new file mode 100644 index 0000000..664726a --- /dev/null +++ b/src/calc.js @@ -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;