I require assistance in determining the average spending for each customer based on an array of purchase records. Below is my data structure:
const purchases = [
{ customer: "Emma", amount: 180 },
{ customer: "Jake", amount: 320 },
{ customer: "Emma", amount: 220 },
{ customer: "Mike", amount: 450 },
{ customer: "Jake", amount: 160 },
{ customer: "Sarah", amount: 90 },
{ customer: "Mike", amount: 250 }
];
I created the following function, but I’m facing issues with it:
function calculateAverageSpending(data) {
const totals = {};
data.forEach(transaction => {
const customer = transaction.customer;
totals[customer] = (totals[customer] || 0) + transaction.amount;
});
const averages = {};
for (const amount in totals) {
if (totals.hasOwnProperty(amount) && typeof amount === 'number') {
averages[amount] = totals[amount] / 2; // Incorrect divisor being used
}
}
return averages;
}
console.log(calculateAverageSpending(purchases));
Currently, this code only returns the total spending for each customer, but I am unsure how to divide this by the correct number of purchases. My goal is to create an output like this:
{
Emma: 200,
Jake: 240,
Mike: 350,
Sarah: 90
}
How can I effectively track both the total spending and the number of purchases for each customer to calculate the accurate average?
quick fix - you don’t need two separate objects. just store both values in one object like {total: 0, count: 0} then divide at the end. also, that typeof amount === 'number' won’t work since object keys are always strings in js.
Your loop’s broken because you’re iterating over amount instead of customer names, and that typeof amount === 'number' check will never work since object keys are always strings. You also need to track how many purchases each customer made. Here’s the fix:
function calculateAverageSpending(data) {
const totals = {};
const counts = {};
data.forEach(transaction => {
const customer = transaction.customer;
totals[customer] = (totals[customer] || 0) + transaction.amount;
counts[customer] = (counts[customer] || 0) + 1;
});
const averages = {};
for (const customer in totals) {
averages[customer] = totals[customer] / counts[customer];
}
return averages;
}
I added a separate counts object to track purchases per customer and fixed the iteration to loop over customer names instead of amounts. That’ll get you the right averages.
Your second loop’s the problem - you’re iterating over amount instead of customer names. Plus hardcoding the divisor as 2 won’t work when customers have different purchase counts. I’ve hit similar data aggregation issues before and reduce() makes this way cleaner:
function calculateAverageSpending(data) {
const customerStats = data.reduce((acc, transaction) => {
const customer = transaction.customer;
if (!acc[customer]) {
acc[customer] = { total: 0, count: 0 };
}
acc[customer].total += transaction.amount;
acc[customer].count += 1;
return acc;
}, {});
const averages = {};
for (const customer in customerStats) {
averages[customer] = customerStats[customer].total / customerStats[customer].count;
}
return averages;
}
This does the data collection in one pass and fixes the variable naming mess from your original code.