vdiachenko/project-lvl1-s328

View on GitHub
src/games/calc.js

Summary

Maintainability
A
0 mins
Test Coverage
import random from 'random';
import gameEngine from '../game-engine';

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

const operations = [
  {
    fn: (a, b) => a + b,
    operation: '+',
  },
  {
    fn: (a, b) => a - b,
    operation: '-',
  },
  {
    fn: (a, b) => a * b,
    operation: '*',
  },
];

const game = () => {
  const num1 = random.int(0, 10);
  const num2 = random.int(0, 10);
  const {
    operation,
    fn,
  } = operations[random.int(0, operations.length - 1)];
  const question = `${num1} ${operation} ${num2}`;
  const answer = fn(num1, num2).toString();

  return {
    question,
    answer,
  };
};

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