|
1 | | -import playGame from '../index.js'; |
| 1 | +import playGame, { getRandomNum } from '../index.js'; |
2 | 2 |
|
3 | 3 | const rules = 'What is the result of the expression?'; |
4 | 4 |
|
5 | | -const calc = () => { |
6 | | - const firstNum = Math.floor(Math.random() * 50) + 1; |
7 | | - const secondNum = Math.floor(Math.random() * 25) + 1; |
8 | | - const operations = ['+', '-', '*']; |
9 | | - const operation = operations[Math.floor(Math.random() * 3)]; |
10 | | - const expression = `${firstNum} ${operation} ${secondNum}`; |
11 | | - let correctAnswer = 0; |
| 5 | +const calc = (operation, a, b) => { |
| 6 | + let result = 0; |
12 | 7 |
|
13 | 8 | switch (operation) { |
14 | 9 | case '+': |
15 | | - correctAnswer = firstNum + secondNum; |
| 10 | + result = a + b; |
16 | 11 | break; |
17 | 12 | case '-': |
18 | | - correctAnswer = firstNum - secondNum; |
| 13 | + result = a - b; |
19 | 14 | break; |
20 | 15 | case '*': |
21 | | - correctAnswer = firstNum * secondNum; |
| 16 | + result = a * b; |
22 | 17 | break; |
23 | 18 | default: |
24 | 19 | break; |
25 | 20 | } |
26 | 21 |
|
| 22 | + return result; |
| 23 | +}; |
| 24 | + |
| 25 | +const getGameData = () => { |
| 26 | + const firstNum = getRandomNum(50) + 1; |
| 27 | + const secondNum = getRandomNum(25) + 1; |
| 28 | + const operations = ['+', '-', '*']; |
| 29 | + const operation = operations[getRandomNum(3)]; |
| 30 | + const expression = `${firstNum} ${operation} ${secondNum}`; |
| 31 | + const correctAnswer = calc(operation, firstNum, secondNum); |
| 32 | + |
27 | 33 | return [expression, String(correctAnswer)]; |
28 | 34 | }; |
29 | 35 |
|
30 | | -export default () => playGame(rules, calc); |
| 36 | +export default () => playGame(rules, getGameData); |
0 commit comments