From b2fb7f95f6a28a143def07b6864ef5845ef4aab8 Mon Sep 17 00:00:00 2001 From: Ayronhayd <128861928+Ayronhayd@users.noreply.github.com> Date: Wed, 28 Feb 2024 20:22:20 +0300 Subject: [PATCH] makeCounter --- js/main.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/js/main.js b/js/main.js index 738ca0d..662a987 100644 --- a/js/main.js +++ b/js/main.js @@ -1,9 +1,5 @@ const OBJECT_COUNT = 25; const fixedValuesFromTo = { - MIN_PHOTOID: 1, - MAX_PHOTOID: 25, - MIN_ID: 1, - MAX_ID: 25, MIN_COMMENTID: 1, MAX_COMMENTID: 9999, MIN_AVATAR: 1, @@ -59,6 +55,17 @@ const DESCRIPTIONS = [ 'Любовь переносит все' ]; +const makeCounter = () => { + let currentCount = 1; + + return function() { + return currentCount++; + }; +}; + +const Id = makeCounter(); +const Photoid = makeCounter(); + const getRandomInteger = (min, max) => { const lower = Math.ceil(Math.min(Math.abs(min), Math.abs(max))); const upper = Math.floor(Math.max(Math.abs(min), Math.abs(max))); @@ -83,8 +90,6 @@ const createRandomIdFromRangeGenerator = (min, max) => { }; }; -const GeneratePhotoId = createRandomIdFromRangeGenerator(fixedValuesFromTo.MIN_PHOTOID, fixedValuesFromTo.MAX_PHOTOID); -const GenerateId = createRandomIdFromRangeGenerator(fixedValuesFromTo.MIN_ID, fixedValuesFromTo.MAX_ID); const GenerateCommentId = createRandomIdFromRangeGenerator(fixedValuesFromTo.MIN_COMMENTID, fixedValuesFromTo.MAX_COMMENTID); const commentInformation = () => ({ @@ -95,8 +100,8 @@ const commentInformation = () => ({ }); const photoDescription = () => ({ - id: GenerateId(), - url: `photos/${GeneratePhotoId()}.jpg`, + id: Id(), + url: `photos/${Photoid()}.jpg`, description: DESCRIPTIONS[getRandomInteger(0, DESCRIPTIONS.length - 1)], likes: getRandomInteger(fixedValuesFromTo.MIN_LIKES, fixedValuesFromTo.MAX_LIKES), comments: Array.from({ @@ -109,4 +114,5 @@ const generatedObjectArrays = () => Array.from({ length: OBJECT_COUNT }, photoDescription); + generatedObjectArrays();