khannanova/project-lvl1-s388

View on GitHub
src/games/calculator.js

Summary

Maintainability
A
0 mins
Test Coverage
import engine from '..';
import getRandomNumber from '../utils';

const description = 'What is the result of the expression?';
const getRandomOperator = () => {
  const operators = '+-*';
  const operator = operators[getRandomNumber(0, operators.length - 1)];
  return operator;
};

const calculate = (firstOperand, secondOperand, operator) => {
  switch (operator) {
    case '+':
      return firstOperand + secondOperand;
    case '-':
      return firstOperand - secondOperand;
    case '*':
      return firstOperand * secondOperand;
    default:
      return 'error';
  }
};

const getGameData = () => {
  const firstOperand = getRandomNumber(1, 10);
  const secondOperand = getRandomNumber(1, 10);
  const operator = getRandomOperator();
  const question = `${firstOperand} ${operator} ${secondOperand}`;
  const answer = String(calculate(firstOperand, secondOperand, operator));
  return { question, answer };
};

export default () => {
  engine(description, getGameData);
};