Skip to content

Commit b05ed32

Browse files
authored
Merge pull request #3 from YouAreNotReady/module4-task1
2 parents 00f890a + e2008cf commit b05ed32

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ <h2 class="data-error__title">Не удалось загрузить данны
234234
</section>
235235
</template>
236236

237+
<script src="/js/main.js"></script>
237238
<script src="/js/functions.js"></script>
238239
</body>
239240
</html>

js/main.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const DESCRIPTIONS = [
2+
'Закат над морем',
3+
'Городская улица ночью',
4+
'Заснеженный лес',
5+
'Кафе на набережной',
6+
'Горная вершина',
7+
'Цветущая сакура',
8+
];
9+
10+
const MESSAGES = [
11+
'Всё отлично!',
12+
'В целом всё неплохо. Но не всё.',
13+
'Когда вы делаете фотографию, хорошо бы убирать палец из кадра. В конце концов это просто непрофессионально.',
14+
'Моя бабушка случайно чихнула с фотоаппаратом в руках и у неё получилась фотография лучше.',
15+
'Я поскользнулся на банановой кожуре и уронил фотоаппарат на кота и у меня получилась фотография лучше.',
16+
'Лица у людей на фотке перекошены, как будто их избивают. Как можно было поймать такой неудачный момент?!',
17+
];
18+
19+
const NAMES = [
20+
'Артем',
21+
'Александр',
22+
'Виктор',
23+
'Василий',
24+
'Петр',
25+
'Анастасия',
26+
'Валерия',
27+
'Виктория',
28+
'Дарья',
29+
'Елизавета',
30+
];
31+
32+
const getRandomInteger = function(min, max) {
33+
const rand = min + Math.random() * (max + 1 - min);
34+
return Math.floor(rand);
35+
};
36+
37+
const createIdGenerator = function(min, max) {
38+
const previousValues = [];
39+
40+
return function() {
41+
let lastGeneratedId = getRandomInteger(min, max);
42+
43+
if(previousValues.length >= (max - min + 1)) {
44+
console.log('Закончились уникальные идентификаторы');
45+
return null;
46+
}
47+
48+
while(previousValues.includes(lastGeneratedId)) {
49+
lastGeneratedId = getRandomInteger(min, max);
50+
}
51+
52+
previousValues.push(lastGeneratedId);
53+
return lastGeneratedId;
54+
};
55+
};
56+
57+
const getRandomArrayElement = (array) => array[getRandomInteger(0, array.length - 1)];
58+
59+
const generatePhotoId = createIdGenerator(1, 25);
60+
61+
const generatePhotoUrlId = createIdGenerator(1, 25);
62+
63+
const generateCommentId = createIdGenerator(1, 750);
64+
65+
const createCommentObject = () => ({
66+
id: generateCommentId(),
67+
avatar: 'img/avatar-' + getRandomInteger(1, 6) + '.svg',
68+
message: getRandomArrayElement(MESSAGES),
69+
name: getRandomArrayElement(NAMES),
70+
});
71+
72+
const createPhotoObject = () => ({
73+
id: generatePhotoId(),
74+
url: 'photos/' + generatePhotoUrlId() + '.jpg',
75+
description: getRandomArrayElement(DESCRIPTIONS),
76+
likes: getRandomInteger(15, 200),
77+
comments: Array.from({length: getRandomInteger(0, 30)}, createCommentObject)
78+
});
79+
80+
const testPhotoObjects = Array.from({length: 25}, createPhotoObject);

0 commit comments

Comments
 (0)