From ed11030cfd10389941db59b551daaf1f758666af Mon Sep 17 00:00:00 2001 From: Mikita Date: Fri, 15 Nov 2024 14:17:38 +0300 Subject: [PATCH] refactor: use lodash random --- package-lock.json | 7 +++++++ package.json | 1 + src/games/GCDGame.js | 6 ++++-- src/games/calcGame.js | 8 +++++--- src/games/evenGame.js | 4 +++- src/games/primeGame.js | 4 +++- src/games/progressionGame.js | 8 +++++--- 7 files changed, 28 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index bdb2cbe..e2c53f5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@eslint/eslintrc": "^3.1.0", + "lodash-es": "^4.17.21", "readline-sync": "^1.4.10" }, "bin": { @@ -1931,6 +1932,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash-es": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==", + "license": "MIT" + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", diff --git a/package.json b/package.json index c926225..808180d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "license": "ISC", "dependencies": { "@eslint/eslintrc": "^3.1.0", + "lodash-es": "^4.17.21", "readline-sync": "^1.4.10" }, "devDependencies": { diff --git a/src/games/GCDGame.js b/src/games/GCDGame.js index b482836..4d55bc1 100644 --- a/src/games/GCDGame.js +++ b/src/games/GCDGame.js @@ -1,9 +1,11 @@ +import { random } from 'lodash-es'; + 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(0, 100); + const b = random(0, 100); askQuestion(`${a} ${b}`); diff --git a/src/games/calcGame.js b/src/games/calcGame.js index ca42832..bf72e87 100644 --- a/src/games/calcGame.js +++ b/src/games/calcGame.js @@ -1,12 +1,14 @@ +import { random } from 'lodash-es'; + 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); diff --git a/src/games/evenGame.js b/src/games/evenGame.js index 68ed7c6..f9a98c3 100644 --- a/src/games/evenGame.js +++ b/src/games/evenGame.js @@ -1,9 +1,11 @@ +import { random } from 'lodash-es'; + 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(0, 1000); askQuestion(num); diff --git a/src/games/primeGame.js b/src/games/primeGame.js index 8c49ce5..0f35dcd 100644 --- a/src/games/primeGame.js +++ b/src/games/primeGame.js @@ -1,9 +1,11 @@ +import { random } from 'lodash-es'; + 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(0, 1000); askQuestion(num); diff --git a/src/games/progressionGame.js b/src/games/progressionGame.js index 56ba3d4..9f2841b 100644 --- a/src/games/progressionGame.js +++ b/src/games/progressionGame.js @@ -1,12 +1,14 @@ +import { random } from 'lodash-es'; + 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] = '..';