meshcherov/project-lvl1-s474

View on GitHub
src/games/calc.js

Summary

Maintainability
A
0 mins
Test Coverage
import playBrainGame from '..';
import { getRandom } from '../utils';
import { cons } from 'hexlet-pairs';

const description = 'What is the result of the expression?';

const getCorrectAnswer = (numOne, numTwo, getMathOperation) => {
  switch (getMathOperation) {
    case '+':
      return numOne + numTwo;

    case '-':
      return numOne - numTwo;

    case '*':
      return numOne * numTwo;

    case '/':
      return numOne / numTwo;

    default:
      return 'Error operation';
  }
};

const getCalcGame = () => {
  const getQuestionAndAnswer = () => {
    const mathOperations = ['+', '-', '*', '/'];
    const numOne = getRandom(1, 100);
    const numTwo = getRandom(1, 100);
    const numMathOperation = getRandom(0, mathOperations.length - 1);
    const mathOperation = mathOperations[numMathOperation];
    const correctAnswer = getCorrectAnswer(numOne, numTwo, mathOperation).toString();

    const question = `${numOne} ${mathOperation} ${numTwo}`;
    return cons(question, correctAnswer);
  };
  playBrainGame(getQuestionAndAnswer, description);
};

export default getCalcGame;