Skip to content

Commit

Permalink
feat: progression game
Browse files Browse the repository at this point in the history
  • Loading branch information
sazanik committed Nov 7, 2024
1 parent 1d5169e commit fba6ccb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions bin/brain-progression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

import brainGames from './brain-games.js';
import { progressionGame } from '../src/games/index.js';

brainGames.startGame(progressionGame, 'What number is missing in the progression?');
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"brain-games": "bin/brain-games.js",
"brain-even": "bin/brain-even.js",
"brain-calc": "bin/brain-calc.js",
"brain-gcd": "bin/brain-gcd.js"
"brain-gcd": "bin/brain-gcd.js",
"brain-progression": "bin/brain-progression.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
1 change: 1 addition & 0 deletions src/games/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as evenGame } from './evenGame.js';
export { default as calcGame } from './calcGame.js';
export { default as gcdGame } from './gcdGame.js';
export { default as progressionGame } from './progressionGame.js';
27 changes: 27 additions & 0 deletions src/games/progressionGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { askQuestion, getAnswer, getResponseAfterRound } from '../cli.js';

export default () => {
const start = Math.round(Math.random() * 30);
const step = Math.round(Math.random() * 20);
const length = 10;

const progression = Array(length).fill(start).map((value, index) => start + index * step);
const targetIndex = Math.floor(Math.random() * length);
const copyProgression = [...progression];

copyProgression[targetIndex] = '..';

const questionText = copyProgression.join(' ');

askQuestion(questionText);

const userAnswer = getAnswer('Your answer: ');
const rightAnswer = progression[targetIndex];

const isUserRight = Number(userAnswer) === rightAnswer;
const resultString = getResponseAfterRound(isUserRight, userAnswer, rightAnswer);

console.log(resultString);

return isUserRight;
};

0 comments on commit fba6ccb

Please sign in to comment.