vnk729/project-lvl1-s454

View on GitHub
src/games/calc.js

Summary

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

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

const getGame = () => {
  const a = randomInt(1, 100);
  const b = randomInt(1, 100);
  const operator = randomInt(1, 3);

  let question;
  let correctAnswer;

  switch (operator) {
    case 1:
      question = `${a} + ${b}`;
      correctAnswer = String(a + b);
      break;
    case 2:
      question = `${a} - ${b}`;
      correctAnswer = String(a - b);
      break;
    case 3:
      question = `${a} * ${b}`;
      correctAnswer = String(a * b);
      break;
    default:
      break;
  }

  return cons(question, correctAnswer);
};

export default () => gameEngine(message, getGame);