Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Функции возвращаются #7

Merged
merged 4 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@ <h2 class="success__title">Изображение успешно загруже
<h2 class="data-error__title">Не удалось загрузить данные</h2>
</section>
</template>
<script type="module" src="./js/main.js"></script>
<script src="./js/functions.js"></script>
</body>
</html>
File renamed without changes.
21 changes: 21 additions & 0 deletions js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,24 @@ getNumbers('а я томат'); // NaN
getNumbers(2023); // 2023
getNumbers(-1); // 1
getNumbers(1.5); // 15

// 4

const timeToMinuites = (time) => {
const [hours, minutes] = time.split(':');
const totalMinutes = parseInt(hours, 10) * 60 + parseInt(minutes, 10);
return totalMinutes;
};
const isMeetingInTime = (dayStart, dayEnd, meetingStart, meetingTime) => {
dayStart = timeToMinuites(dayStart);
dayEnd = timeToMinuites(dayEnd);
meetingStart = timeToMinuites(meetingStart);
return meetingStart >= dayStart && meetingStart + meetingTime <= dayEnd;
};


isMeetingInTime('08:00', '17:30', '14:00', 90); // true
isMeetingInTime('8:0', '10:0', '8:0', 120); // true
isMeetingInTime('08:00', '14:30', '14:00', 90); // false
isMeetingInTime('14:00', '17:30', '08:0', 90); // false
isMeetingInTime('8:00', '17:30', '08:00', 900); // false
2 changes: 1 addition & 1 deletion js/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createMockPosts } from './posts';
import { createMockPosts } from './mock';

createMockPosts();
37 changes: 37 additions & 0 deletions js/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createRandomId, getRandomInteger } from './utils';
import { printPosts } from './posts';
import { POSTS_DATA } from './data';

const { POSTS_COUNT, NAMES, DESCRIPTIONS, COMMENTS } = POSTS_DATA;

const generatePhotoId = createRandomId(1, POSTS_COUNT);
const generateCommentId = createRandomId(1, 30 * POSTS_COUNT);

const createSingleComment = () => ({
id: generateCommentId(),
avatar: `img/avatar-${getRandomInteger(1, 6)}.svg`,
message: COMMENTS[getRandomInteger(0, COMMENTS.length - 1)],
name: NAMES[getRandomInteger(0, NAMES.length - 1)]
});

const createComments = () => Array.from({length: getRandomInteger(0, 30)}, createSingleComment);

const createPhotoPost = () => {
const id = generatePhotoId();
return {
id: id,
url: `photos/${id}.jpg`,
description: DESCRIPTIONS[getRandomInteger(0, DESCRIPTIONS.length - 1)],
likes: getRandomInteger(15, 200),
comments: createComments(),
};
};

const createMockPosts = () => {
const data = Array.from({length: POSTS_COUNT}, createPhotoPost);

printPosts(data);
};


export {createMockPosts};
46 changes: 15 additions & 31 deletions js/posts.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
import { createRandomId, getRandomInteger } from './utils';
import { POSTS_DATA } from './mock-data';
const picturesContainer = document.querySelector('.pictures');
const pictureTemplate = document.querySelector('#picture').content.querySelector('.picture');
const postsFragment = document.createDocumentFragment();

const { POSTS_COUNT, NAMES, DESCRIPTIONS, COMMENTS } = POSTS_DATA;

const generatePhotoId = createRandomId(1, POSTS_COUNT);
const generateCommentId = createRandomId(1, 30 * POSTS_COUNT);

const createSingleComment = () => ({
id: generateCommentId(),
avatar: `img/avatar-${getRandomInteger(1, 6)}.svg`,
message: COMMENTS[getRandomInteger(0, COMMENTS.length - 1)],
name: NAMES[getRandomInteger(0, NAMES.length - 1)]
});

const createComments = () => Array.from({length: getRandomInteger(0, 30)}, createSingleComment);

const createPhotoPost = () => {
const id = generatePhotoId();
return {
id: id,
url: `photos/${id}.jpg`,
description: DESCRIPTIONS[getRandomInteger(0, DESCRIPTIONS.length - 1)],
likes: getRandomInteger(15, 200),
comments: createComments(),
};
};

const createMockPosts = () => {
const data = Array.from({length: POSTS_COUNT}, createPhotoPost);
return data;
const printPosts = (data) => {
data.forEach((post) => {
const singlePost = pictureTemplate.cloneNode(true);
const singlePostImg = singlePost.querySelector('img');
singlePostImg.src = post.url;
singlePostImg.alt = post.description;
singlePost.querySelector('.picture__likes').innerText = post.likes;
singlePost.querySelector('.picture__comments').append(post.comments.length);
postsFragment.append(singlePost);
});
picturesContainer.append(postsFragment);
};

export {createMockPosts};
export {printPosts};
8 changes: 4 additions & 4 deletions js/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function getRandomInteger (min, max) {
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)));
const result = Math.random() * (upper - lower + 1) + lower;

return Math.floor(result);
}
};

function createRandomId (min, max) {
const createRandomId = (min, max) => {
const previousValues = [];

return function () {
Expand All @@ -20,6 +20,6 @@ function createRandomId (min, max) {
previousValues.push(currentValue);
return currentValue;
};
}
};

export {createRandomId, getRandomInteger};
Loading