-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0bf69e
commit 2ebe031
Showing
3 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |