const transactions = [
{ userId: 'u1', category: 'Electronics', amount: 100, currency: 'USD', date: '2026-08-25' },
{ userId: 'u2', category: 'Books', amount: 1500, currency: 'RUB', date: '2026-08-25' },
{ userId: 'u1', category: 'Electronics', amount: 80, currency: 'EUR', date: '2026-08-26' },
{ userId: 'u3', category: 'Clothes', amount: 120, currency: 'USD', date: '2026-08-26' },
{ userId: 'u2', category: 'Electronics', amount: 50, currency: 'USD', date: '2026-08-27' },
{ userId: 'u3', category: 'Clothes', amount: 4000, currency: 'RUB', date: '2026-08-28' },
];
const rates = {
USD: 90, // 1 USD = 90 RUB
EUR: 100, // 1 EUR = 100 RUB
RUB: 1, // 1 RUB = 1 RUB
};
function getSalesAnalytics(transactions, exchangeRates) {
const userChecks = {}
return transactions.reduce(
(result, cur) => {
const trPrice =
cur.currency === 'RUB' ? cur.amount : cur.amount * exchangeRates[cur.currency];
result.totalRevenueRu += trPrice;
const categoryStats = result.categoryStats;
const category = cur.category;
if (categoryStats[category]) {
categoryStats[category].totalSpent += trPrice;
categoryStats[category].count += 1;
} else {
categoryStats[category] = {
count: 1,
totalSpent: trPrice,
};
}
if(userChecks[cur.userId]){
userChecks[cur.userId] += trPrice
}else {
userChecks[cur.userId] = trPrice
}
if (userChecks[cur.userId] > 10000 && !result.vipCustomers.includes(cur.userId)) {
result.vipCustomers.push(cur.userId);
}
return result;
},
{
totalRevenueRu: 0,
categoryStats: {},
vipCustomers: [],
}
);
}
const report = getSalesAnalytics(transactions, rates);
console.log(report);
// {
// totalRevenueRu: 34500, // Общая сумма в рублях
// categoryStats: {
// Electronics: { totalSpent: 21500, count: 3 }, // (100*90) + (80*100) + (50*90)
// Books: { totalSpent: 1500, count: 1 },
// Clothes: { totalSpent: 14800, count: 2 } // (120*90) + 4000
// },
// vipCustomers: ['u1', 'u3'] // u1 потратил 17000, u3 потратил 14800, u2 только 6000
// }
Code will run automatically after changes.
Code runs in an isolated environment.
Code runs in an isolated environment.