Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions tasks/task1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Задача 1: Написати функцію, яка повертає найпопулярніші товарні категорії на основі продажів.
// Спочатку фільтруємо товари, які продані більше 5 разів, потім сортуємо їх за кількістю продажів,
// а потім залишаємо тільки унікальні категорії товарів.
"ВИКОРИСТОВУВАТИ ЛИШЕ МЕТОДИ МАСИВІВ filter, map, sort. Для того щоб залишити лише унікальні категорії товарів можна використати конструкцію new Set, або використати метод filter()/indexOf()/forEach()"

function getPopularCategories(products) {
//Ваш код

const filteredProducts = products.filter(product => product.sales > 5);


const sortedProducts = filteredProducts.sort((a, b) => b.sales - a.sales);


const categories = sortedProducts.map(product => product.category);


const uniqueCategories = [...new Set(categories)];

return uniqueCategories;
}

// Приклад використання:
const products = [
{ name: 'Phone', category: 'Electronics', sales: 10 },
{ name: 'Tablet', category: 'Electronics', sales: 3 },
Expand All @@ -18,5 +24,5 @@ const products = [
{ name: 'Watch', category: 'Electronics', sales: 7 }
];

console.log(getPopularCategories(products)); // ['Electronics', 'Clothing', 'Footwear']
module.exports = getPopularCategories;
console.log(getPopularCategories(products));
module.exports = getPopularCategories;