1
0
Files
2024-05-10 10:38:11 +03:00

129 lines
3.5 KiB
JavaScript

function generateRandomDate() {
const year = Math.floor(Math.random() * (2024 - 1900)) + 1900;
const month = Math.floor(Math.random() * 12) + 1;
const day = Math.floor(Math.random() * 28) + 1;
return new Date(year, month - 1, day);
}
function generateRandomAuthor() {
const names = [
'Александр',
'Екатерина',
'Дмитрий',
'Анна',
'Иван',
'Мария',
'Сергей',
'Ольга',
'Павел',
'Наталья',
];
const surnames = [
'Иванов',
'Петров',
'Смирнов',
'Кузнецов',
'Васильев',
'Соколов',
'Михайлов',
'Новиков',
'Федоров',
'Морозов',
];
const randomName = names[Math.floor(Math.random() * names.length)];
const randomSurname = surnames[Math.floor(Math.random() * surnames.length)];
return {
name: randomName,
surname: randomSurname,
};
}
function generateRandomBook() {
return {
title: 'Книга ' + Math.floor(Math.random() * 1000),
genre: [
'Фантастика',
'Роман',
'Детектив',
'Фэнтези',
'Приключения',
'Исторический роман',
'Триллер',
'Поэзия',
'Научно-популярная литература',
'Юмористическая проза',
][Math.floor(Math.random() * 6)],
publishDate: generateRandomDate(),
author: generateRandomAuthor(),
pageCount: Math.floor(Math.random() * 500) + 100,
};
}
function createBooksArray(N) {
const books = [];
for (let i = 0; i < N; i++) {
books.push(generateRandomBook());
}
return books;
}
function findBooksByAuthor(books, authorLastName) {
const filteredBooks = books.filter((book) => {
return book.author.surname === authorLastName;
});
return filteredBooks;
}
function findBooksByGenre(books, genre) {
const filteredBooks = books.filter((book) => book.genre === genre);
return filteredBooks;
}
function findBooksByPublishYearRange(books, startYear, endYear) {
const filteredBooks = books.filter((book) => {
const publishYear = book.publishDate.getFullYear();
return publishYear >= startYear && publishYear <= endYear;
});
return filteredBooks;
}
function removeBooksPageCount(books, minPageCount) {
for (let i = 0; i < books.length; i++) {
if (books[i].pageCount < minPageCount) {
delete books[i].pageCount;
}
}
return books;
}
function addAgeInfoToBooks(books) {
const currentDate = new Date();
books.forEach((book) => {
const yearsSincePublication =
currentDate.getFullYear() - book.publishDate.getFullYear();
book.age = yearsSincePublication;
});
return books;
}
// const booksArray = createBooksArray(10);
// console.log('Initial array of books:');
// displayBooks(booksArray);
// findBooksByAuthor(booksArray, 'Smith');
// findBooksByGenre(booksArray, 'Fantasy');
// findBooksByPublishYearRange(booksArray, 2000, 2010);
// booksArray = removeBooksByPageCount(booksArray, 200);
// console.log('Array after removing books by page count:');
// displayBooks(booksArray);
// booksArray = addAgeInfoToBooks(booksArray);
// console.log('Array after adding age info:');
// displayBooks(booksArray);