From 21419baf9730e675099716c2ad202bfa4916f0e6 Mon Sep 17 00:00:00 2001 From: Aleksandr Anokhin Date: Fri, 4 Oct 2024 20:44:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=20=D0=BD=D0=B0=D0=B7=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B9=20=D0=B4=D0=BB=D1=8F=20=D1=84=D0=BE?= =?UTF-8?q?=D1=82=D0=BE,=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=9D=D0=B5=D0=BF=D0=BE=D0=B2=D1=82=D0=BE=D1=80=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=D0=BE=D1=81=D1=82=D1=8C=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/main.js | 60 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/js/main.js b/js/main.js index e9060b9..bab4c55 100644 --- a/js/main.js +++ b/js/main.js @@ -1,15 +1,19 @@ -function getRandomIntInclusive(min, max) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min + 1) + min); -} +const PHOTO_COUNT = 25; -const MESSAGE_TEXT = [ +const MESSAGES = [ 'Всё отлично!', 'В целом всё неплохо. Но не всё.', 'У моего кота получилась фотография лучше.', ]; +const DESCRIPTIONS = [ + 'Утро!', + 'Котик', + 'Солнышко', + 'Красивое фото', + 'Что-то новенькое', +]; + const NAMES = [ 'Иван', 'Себастьян', @@ -19,23 +23,45 @@ const NAMES = [ 'Юлия', ]; +const getRandomIntInclusive = (min, max) => { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1) + min); +}; + +const counter = () => { + let sum = 0; + return () => { + sum = sum + 1; + return sum; + }; +}; + +const uniquePhoto = counter(); +const uniqueId = counter(); const getRandomArrayElement = (elements) => elements[getRandomIntInclusive(0, elements.length - 1)]; const createComments = () => ({ id: getRandomIntInclusive(0, 30), - avatar: 'img/avatar-'+ getRandomIntInclusive(1, 6) + '.svg', - message: getRandomArrayElement(MESSAGE_TEXT), + avatar: `img/avatar-${getRandomIntInclusive(1, 6)}.svg`, + message: getRandomArrayElement(MESSAGES), name: getRandomArrayElement(NAMES), }); -const createPhotoObjects = () => ({ - id: getRandomIntInclusive(1, 25), - url: 'photos/' + getRandomIntInclusive(1, 25) + '.jpg', - description: 'Новая фотография', - likes: getRandomIntInclusive(15, 200), - comments: createComments(), -}); -console.log ( - createPhotoObjects() +const createPhoto = () => { + const id = uniqueId(); + return { + id: id, + url: `photos/${uniquePhoto()}.jpg`, + description: getRandomArrayElement(DESCRIPTIONS), + likes: getRandomIntInclusive(15, 200), + comments: Array.from({ length: getRandomIntInclusive(0, 30) }, createComments), + }; +}; + +const createPhotos = () => Array.from({ length: PHOTO_COUNT }, createPhoto); + +console.log( + createPhotos() );