-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain-calc.js
37 lines (32 loc) · 1.19 KB
/
brain-calc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import generateRandomNum from '../generaterandomnum.js';
import { getEngine, roundsCount } from '../index.js';
const gameRules = 'What is the result of the expression?';
const operators = ['+', '-', '*'];
const logicsOfMath = (firstRandomNum, secondRandomNum, randomOperator) => {
switch (randomOperator) {
case '+':
return firstRandomNum + secondRandomNum;
case '-':
return firstRandomNum - secondRandomNum;
case '*':
return firstRandomNum * secondRandomNum;
default:
throw new Error(`operation ${randomOperator} is not supported`);
}
};
const generateOneRound = () => {
const firstRandomNum = generateRandomNum(1, 100);
const secondRandomNum = generateRandomNum(1, 100);
const randomOperator = (operators[(generateRandomNum(0, operators.length - 1))]);
const question = (`${firstRandomNum} ${randomOperator} ${secondRandomNum}`);
const answer = (`${logicsOfMath(firstRandomNum, secondRandomNum, randomOperator)}`);
return [question, answer];
};
const calculatorGame = () => {
const gameData = [];
for (let i = 0; i < roundsCount; i += 1) {
gameData.push(generateOneRound());
}
getEngine(gameRules, gameData);
};
export default calculatorGame;