diff --git a/index.html b/index.html index a7b1eff..7f85df0 100644 --- a/index.html +++ b/index.html @@ -233,6 +233,6 @@

Изображение успешно загруже

Не удалось загрузить данные

- + diff --git a/js/mock-data.js b/js/data.js similarity index 100% rename from js/mock-data.js rename to js/data.js diff --git a/js/functions.js b/js/functions.js index 2d9f055..e7c02a2 100644 --- a/js/functions.js +++ b/js/functions.js @@ -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 diff --git a/js/main.js b/js/main.js index e2d04b3..1535817 100644 --- a/js/main.js +++ b/js/main.js @@ -1,3 +1,3 @@ -import { createMockPosts } from './posts'; +import { createMockPosts } from './mock'; createMockPosts(); diff --git a/js/mock.js b/js/mock.js new file mode 100644 index 0000000..ab30588 --- /dev/null +++ b/js/mock.js @@ -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}; diff --git a/js/posts.js b/js/posts.js index af96ce4..640d61e 100644 --- a/js/posts.js +++ b/js/posts.js @@ -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}; diff --git a/js/utils.js b/js/utils.js index 4456f8b..c4b1eb7 100644 --- a/js/utils.js +++ b/js/utils.js @@ -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 () { @@ -20,6 +20,6 @@ function createRandomId (min, max) { previousValues.push(currentValue); return currentValue; }; -} +}; export {createRandomId, getRandomInteger};