From 2ebe031af9a5f89de16d558ce719599e668cec54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=90=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=B5=D0=B2=D0=B0?= Date: Wed, 11 Oct 2023 18:30:14 +0300 Subject: [PATCH] a --- Makefile | 6 +++++- bin/brain-calc.js | 5 +++++ src/calc.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 bin/brain-calc.js create mode 100644 src/calc.js 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;