-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const COMMENT_MESSAGES = [ | ||
'Всё отлично!', | ||
'В целом всё неплохо. Но не всё.', | ||
'Когда вы делаете фотографию, хорошо бы убирать палец из кадра. В конце концов это просто непрофессионально.', | ||
'Моя бабушка случайно чихнула с фотоаппаратом в руках и у неё получилась фотография лучше.', | ||
'Я поскользнулся на банановой кожуре и уронил фотоаппарат на кота и у меня получилась фотография лучше.', | ||
'Лица у людей на фотке перекошены, как будто их избивают. Как можно было поймать такой неудачный момент?!' | ||
]; | ||
|
||
const NAMES = [ | ||
'Иван', | ||
'Хуан Себастьян', | ||
'Мария', | ||
'Кристоф', | ||
'Виктор', | ||
'Юлия', | ||
'Люпита', | ||
'Вашингтон', | ||
]; | ||
|
||
const MAX_ID_FOR_PHOTOS = 25; | ||
|
||
function createIdGenerator() { | ||
let lastGeneratedId = 0; | ||
|
||
return function() { | ||
return lastGeneratedId++; | ||
}; | ||
} | ||
|
||
function 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))); | ||
const result = Math.random() * (upper - lower + 1) + lower; | ||
return Math.floor(result); | ||
} | ||
|
||
const getRandomArrayElement = (elements) => | ||
elements[getRandomInteger(0, elements.length - 1)]; | ||
|
||
function createArrayComments(count) { | ||
const commentsArray = []; | ||
const generateCommentId = createIdGenerator(); | ||
while (commentsArray.length < count) { | ||
const comment = { | ||
id: generateCommentId(), | ||
avatar: `img/avatar-${getRandomInteger(1, 6)}.svg`, | ||
message: `${getRandomArrayElement(COMMENT_MESSAGES)}`, | ||
name: `${getRandomArrayElement(NAMES)}` | ||
}; | ||
|
||
if (getRandomInteger(1, 2) === 2) { | ||
let secondMessage = `${getRandomArrayElement(COMMENT_MESSAGES)}`; | ||
while (comment.message === secondMessage) { | ||
secondMessage = `${getRandomArrayElement(COMMENT_MESSAGES)}`; | ||
} | ||
comment.message += ` ${secondMessage}`; | ||
} | ||
|
||
commentsArray.push(comment); | ||
} | ||
|
||
return commentsArray; | ||
} | ||
|
||
function createArrayPhotos() { | ||
const generatePhotoId = createIdGenerator(); | ||
const photoIdForUrl = createIdGenerator(); | ||
const photosArray = []; | ||
while (photosArray.length < MAX_ID_FOR_PHOTOS) { | ||
const photo = { | ||
id: generatePhotoId(), | ||
url: `photos/${photoIdForUrl()}.jpg`, | ||
description: 'Еще не придумал что здесь написать', | ||
likes: getRandomInteger(15, 200), | ||
comments: createArrayComments(getRandomInteger(1, 30)) | ||
}; | ||
photosArray.push(photo); | ||
} | ||
return photosArray; | ||
} | ||
|
||
createArrayPhotos(); |