Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Khrulev committed Nov 24, 2024
1 parent a636aac commit 37b65d5
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 38 deletions.
File renamed without changes.
45 changes: 45 additions & 0 deletions js/functions.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
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};
49 changes: 16 additions & 33 deletions js/posts.js
Original file line number Diff line number Diff line change
@@ -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};
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};

0 comments on commit 37b65d5

Please sign in to comment.