Skip to content

Commit

Permalink
refactored function
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil888-m committed Nov 28, 2024
1 parent 17ff33d commit cbe762f
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const NAMES = [
'Алекс',
'Коля',
'Ксения',
'Алина'
'Алина',
];
const DESCRIPTIONS = [
'Солнечный берег с белым песком и пальмами.',
Expand All @@ -25,16 +25,17 @@ const DESCRIPTIONS = [
'Спокойное озеро с отражением облаков на воде.',
'Уличный рынок с яркими фруктами и овощами.',
'Групповой снимок друзей на пляже.',
'Кошка, спящая на окне в солнечный день.'
'Кошка, спящая на окне в солнечный день.',
];
const MESSAGES = [
'Всё отлично!',
'В целом всё неплохо.Но не всё.',
'Когда вы делаете фотографию, хорошо бы убирать палец из кадра.В конце концов это просто непрофессионально.',
'Моя бабушка случайно чихнула с фотоаппаратом в руках и у неё получилась фотография лучше.',
'Я поскользнулся на банановой кожуре и уронил фотоаппарат на кота и у меня получилась фотография лучше.',
'Лица у людей на фотке перекошены, как будто их избивают.Как можно было поймать такой неудачный момент ? !'
'Лица у людей на фотке перекошены, как будто их избивают.Как можно было поймать такой неудачный момент ? !',
];
const PHOTOS_COUNT = 25;
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
Expand All @@ -45,7 +46,7 @@ function getUniqueId(min, max) {
const receivedId = [];

return function () {
if (receivedId.length >= (max - min + 1)) {
if (receivedId.length >= max - min + 1) {
return receivedId[receivedId.length - 1];
}
let currentId = getRandomInt(min, max);
Expand All @@ -62,35 +63,41 @@ function getRandomElement(elements) {
const getPhotoId = getUniqueId(1, 25);
const getCommentId = getUniqueId(1, 1000);
function createPhoto() {

const photoId = getPhotoId();
const comments = [];
const comments = getComments();

for (let j = 0; j < getRandomInt(0, 30); j++) {
const commentId = getCommentId();
const comment = {
id: commentId,
avatar: `img/avatar-${getRandomInt(1, 6)}.svg`,
message: getRandomElement(MESSAGES),
name: getRandomElement(NAMES),
};
comments.push(comment);
}
const photo = {
id: photoId,
url: `photos/${photoId}.jpg`,
description: getRandomElement(DESCRIPTIONS),
likes: getRandomInt(15, 200),
comments: comments
comments,
};

return photo;
}

function getPhotos() {
const photos = Array.from({ length: 25 }, createPhoto);
const photos = Array.from({ length: PHOTOS_COUNT }, createPhoto);
return photos;
}

getPhotos();
function createComment() {
const commentId = getCommentId();
const comment = {
id: commentId,
avatar: `img/avatar-${getRandomInt(1, 6)}.svg`,
message: getRandomElement(MESSAGES),
name: getRandomElement(NAMES),
};

return comment;
}

function getComments() {
const comments = Array.from({ length: getRandomInt(0, 30) }, createComment);
return comments;
}

console.log(getPhotos());

0 comments on commit cbe762f

Please sign in to comment.