From 96c2c47380b2537c3717b35cb425b4772b20946e Mon Sep 17 00:00:00 2001 From: Nikita Khrulev Date: Fri, 22 Nov 2024 22:45:52 +0300 Subject: [PATCH 1/3] minor fix --- js/posts.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/posts.js b/js/posts.js index 68cab77..41d31c8 100644 --- a/js/posts.js +++ b/js/posts.js @@ -31,4 +31,5 @@ const createMockPosts = () => { return data; }; + export {createMockPosts}; From a636aac8271acb53e72beb37359dce25d92b14cc Mon Sep 17 00:00:00 2001 From: Nikita Khrulev Date: Sat, 23 Nov 2024 23:31:40 +0300 Subject: [PATCH 2/3] module5-task2 --- index.html | 2 +- js/functions.js | 54 ++++++++++++++----------------------------------- 2 files changed, 16 insertions(+), 40 deletions(-) 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/functions.js b/js/functions.js index 2d9f055..c2686fe 100644 --- a/js/functions.js +++ b/js/functions.js @@ -1,42 +1,18 @@ -// 1 -const checkLength = (str, length) => str.length <= length; -checkLength('проверяемая строка', 20); // true -checkLength('проверяемая строка', 18); // true -checkLength('проверяемая строка', 10); // false - -// 2 -const isPalindrome = (str) => { - str = str.replace(/ /g, '').toLowerCase(); - let a = 0; - let b = str.length - 1; - while (a <= b) { - if (str[a] === str[b]) { - a++; - b--; - } else { - return false; - } - } - return true; +const timeToMinuites = (time) => { + const [hours, minutes] = time.split(':'); + const totalMinutes = parseInt(hours, 10) * 60 + parseInt(minutes, 10); + return totalMinutes; }; - -isPalindrome('топот'); // true -isPalindrome('ДовОд'); // true -isPalindrome('Кекс'); // false -isPalindrome('Лёша на полке клопа нашёл'); // true - -// 3 -const getNumbers = (input) => { - const str = input.toString(); - const numbers = str.match(/\d/g); - return numbers ? parseInt(numbers.join(''), 10) : NaN; +const isMeetingInTime = (dayStart, dayEnd, meetingStart, meetingTime) => { + dayStart = timeToMinuites(dayStart); + dayEnd = timeToMinuites(dayEnd); + meetingStart = timeToMinuites(meetingStart); + return meetingStart >= dayStart && meetingStart + meetingTime <= dayEnd; }; -getNumbers('2023 год'); // 2023 -getNumbers('ECMAScript 2022'); // 2022 -getNumbers('1 кефир, 0.5 батона'); // 105 -getNumbers('агент 007'); // 7 -getNumbers('а я томат'); // NaN -getNumbers(2023); // 2023 -getNumbers(-1); // 1 -getNumbers(1.5); // 15 + +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 From 37b65d57c9c97241fa2125033a191669ad208570 Mon Sep 17 00:00:00 2001 From: Nikita Khrulev Date: Sun, 24 Nov 2024 22:27:36 +0300 Subject: [PATCH 3/3] fix --- js/{mock-data.js => data.js} | 0 js/functions.js | 45 +++++++++++++++++++++++++++++++++ js/main.js | 2 +- js/mock.js | 37 +++++++++++++++++++++++++++ js/posts.js | 49 ++++++++++++------------------------ js/utils.js | 8 +++--- 6 files changed, 103 insertions(+), 38 deletions(-) rename js/{mock-data.js => data.js} (100%) create mode 100644 js/mock.js 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 c2686fe..e7c02a2 100644 --- a/js/functions.js +++ b/js/functions.js @@ -1,3 +1,48 @@ +// 1 +const checkLength = (str, length) => str.length <= length; +checkLength('проверяемая строка', 20); // true +checkLength('проверяемая строка', 18); // true +checkLength('проверяемая строка', 10); // false + +// 2 +const isPalindrome = (str) => { + str = str.replace(/ /g, '').toLowerCase(); + let a = 0; + let b = str.length - 1; + while (a <= b) { + if (str[a] === str[b]) { + a++; + b--; + } else { + return false; + } + } + return true; +}; + +isPalindrome('топот'); // true +isPalindrome('ДовОд'); // true +isPalindrome('Кекс'); // false +isPalindrome('Лёша на полке клопа нашёл'); // true + +// 3 +const getNumbers = (input) => { + const str = input.toString(); + const numbers = str.match(/\d/g); + return numbers ? parseInt(numbers.join(''), 10) : NaN; +}; + +getNumbers('2023 год'); // 2023 +getNumbers('ECMAScript 2022'); // 2022 +getNumbers('1 кефир, 0.5 батона'); // 105 +getNumbers('агент 007'); // 7 +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); 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 3f5549d..640d61e 100644 --- a/js/posts.js +++ b/js/posts.js @@ -1,35 +1,18 @@ -import { createRandomId, getRandomInteger } from './utils'; -import { POSTS_DATA } from './mock-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); - return data; +const picturesContainer = document.querySelector('.pictures'); +const pictureTemplate = document.querySelector('#picture').content.querySelector('.picture'); +const postsFragment = document.createDocumentFragment(); + +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};