Eserian/brain-games

View on GitHub
src/games/progression.js

Summary

Maintainability
A
0 mins
Test Coverage
import gameFlow from '../gameFlow';
import getRandomNum from '../utils';

const rule = 'What number is missing in this progression?';

const progressionLength = 10;

const arifmProgressionGen = (start, step, length) => {
  const result = [];

  for (let i = 0; i < length; i += 1) {
    result[i] = start + step * i;
  }

  return result;
};

const makeQuestion = (arr, indexToChange) => arr.map((item, i) => (i === indexToChange ? '..' : item));

const progressionSet = () => {
  const progressionStart = getRandomNum(1, 100);
  const progressionStep = getRandomNum(2, 10);
  const progression = arifmProgressionGen(progressionStart, progressionStep, progressionLength);
  const numForQuestion = getRandomNum(0, 9);
  const question = makeQuestion(progression, numForQuestion);
  const answer = String(progression[numForQuestion]);

  return { question, answer };
};

export default () => gameFlow(rule, progressionSet);