4.16. Больше деталей#3
Conversation
|
|
||
| const names = ['Николай', 'Дмитрий', 'Василий', 'Харитон', 'Алексей', 'София']; | ||
|
|
||
| const getRandomPositiveInteger = (a, b) => { |
There was a problem hiding this comment.
Можно сократить до getRandomInteger, а то со словом Positive название получается слишком длинное
| return Math.floor(result); | ||
| }; | ||
|
|
||
| const getRandomArrayElement = (array) => |
There was a problem hiding this comment.
Вместо Element можно Item, так как слово Element обычно зарезервировано для DOM-элементов
| }; | ||
|
|
||
| const getRandomArrayElement = (array) => | ||
| array[getRandomPositiveInteger(0, array.length - 1)]; |
There was a problem hiding this comment.
Последний элемент массива еще можно получить вот так array.at(-1)
|
|
||
| const createMessage = () => { | ||
| const arr = []; | ||
| for (let i = 1; i <= getRandomPositiveInteger(1, 2); i++) { |
There was a problem hiding this comment.
Цикл for лучше заменить на for of
| }; | ||
|
|
||
| const createComment = (index) => ({ | ||
| id: index, |
There was a problem hiding this comment.
Вместо index можно crypto.randomUUID()
|
|
||
| const createArrayComments = () => { | ||
| const arr = []; | ||
| for (let i = 1; i <= getRandomPositiveInteger(0, 30); i++) { |
There was a problem hiding this comment.
Как и выше лучше заменить на for ... of
| }; | ||
|
|
||
| const createPictureItem = (index) => ({ | ||
| id: index, |
There was a problem hiding this comment.
как и выше можно заменить на crypto.randomUUID()
| comments: createArrayComments() | ||
| }); | ||
|
|
||
| const createArrayPictures = () => { |
There was a problem hiding this comment.
Создание массива можно заменить на реализацию Фабричного метода (это паттерн проектирования такой) для массиво в js
Array.from({length: 25}, createPictureItem);
|
|
||
| const createArrayPictures = () => { | ||
| const arr = []; | ||
| for (let i = 1; i <= 25; i++) { |
There was a problem hiding this comment.
25 - магическое число. Нужно завести под него константу
No description provided.