Skip to content

Commit

Permalink
refactor: use lodash random
Browse files Browse the repository at this point in the history
  • Loading branch information
sazanik committed Nov 15, 2024
1 parent 5914b44 commit a584756
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"license": "ISC",
"dependencies": {
"@eslint/eslintrc": "^3.1.0",
"lodash": "^4.17.21",
"readline-sync": "^1.4.10"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/games/GCDGame.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import _ from 'lodash';

import { askQuestion, getAnswer, getResponseAfterRound } from '../cli.js';
import { findGCD } from '../utils/index.js';

export default () => {
const a = Math.round(Math.random() * 100);
const b = Math.round(Math.random() * 100);
const a = _.random(1, 100);
const b = _.random(1, 100);

askQuestion(`${a} ${b}`);

Expand Down
8 changes: 5 additions & 3 deletions src/games/calcGame.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import _ from 'lodash';

import { askQuestion, getAnswer, getResponseAfterRound } from '../cli.js';

const operators = ['+', '-', '*'];

export default () => {
const a = Math.round(Math.random() * 10);
const b = Math.round(Math.random() * 10);
const a = _.random(0, 10);
const b = _.random(0, 10);

const operator = operators[Math.floor(Math.random() * operators.length)];
const operator = operators[_.random(0, operators.length - 1)];
const expression = `${a} ${operator} ${b}`;

askQuestion(expression);
Expand Down
4 changes: 3 additions & 1 deletion src/games/evenGame.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import _ from 'lodash';

import { BOOLEAN_TO_ANSWER_MAP } from '../constants/index.js';
import { askQuestion, getAnswer, getResponseAfterRound } from '../cli.js';
import { isEven } from '../utils/index.js';

export default () => {
const num = Math.round(Math.random() * 1000);
const num = _.random(1, 1000);

askQuestion(num);

Expand Down
4 changes: 3 additions & 1 deletion src/games/primeGame.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import _ from 'lodash';

import { askQuestion, getAnswer, getResponseAfterRound } from '../cli.js';
import { isPrime } from '../utils/index.js';
import { BOOLEAN_TO_ANSWER_MAP } from '../constants/index.js';

export default () => {
const num = Math.round(Math.random() * 1000);
const num = _.random(1, 1000);

askQuestion(num);

Expand Down
8 changes: 5 additions & 3 deletions src/games/progressionGame.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import _ from 'lodash';

import { askQuestion, getAnswer, getResponseAfterRound } from '../cli.js';

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

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

copyProgression[targetIndex] = '..';
Expand Down

0 comments on commit a584756

Please sign in to comment.