nzleonid/project-lvl1-s400

View on GitHub
src/games/calc.js

Summary

Maintainability
A
0 mins
Test Coverage
import gameEngine from '..';
import randomNum from '../utils';

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

const operationList = '+-*';

const calc = () => {
  const x = randomNum(1, 100);
  const y = randomNum(1, 100);
  const operation = operationList[randomNum(0, operationList.length)];
  let rightAnswer;
  let question;
  switch (operation) {
    case '+': {
      question = `${x} + ${y}`;
      rightAnswer = String(x + y);
      break;
    }
    case '-': {
      question = `${x} - ${y}`;
      rightAnswer = String(x - y);
      break;
    }
    case '*': {
      question = `${x} * ${y}`;
      rightAnswer = String(x * y);
      break;
    }
    default:
      rightAnswer = 'Error';
  }
  return [question, rightAnswer];
};

export default () => gameEngine(description, calc);