Here's an extended discussion of the code for the Sales Performance Analyzer, explaining each line in detail:
Code:
function calculateAverageSales(sales) {
const totalSales = sales.reduce((sum, sale) => sum + sale, 0);
return totalSales / sales.length;
}
-
function calculateAverageSales(sales) {
- This line defines a function named
calculateAverageSales
that takes an array of sales as its input (sales
). This function will calculate the average sales from the array.
- This line defines a function named
-
const totalSales = sales.reduce((sum, sale) => sum + sale, 0);
- This line declares a constant variable
totalSales
and uses thereduce()
method to sum all the values in thesales
array. sales.reduce()
is a higher-order function that iterates through each element of the array and accumulates the values.(sum, sale) => sum + sale
: This is an arrow function wheresum
holds the running total, andsale
is the current element in the array. The function returns the sum of all elements in the array.- The second argument to
reduce()
is0
, which initializessum
to 0 for the first iteration.
- This line declares a constant variable
-
return totalSales / sales.length;
- This line calculates the average by dividing the total sum of sales by the number of sales (
sales.length
) and returns the result. sales.length
is the number of elements in the sales array, which ensures that the function divides the total by the correct count of sales.
- This line calculates the average by dividing the total sum of sales by the number of sales (
Code:
function determinePerformanceRating(averageSales) {
if (averageSales > 10000) {
return "Excellent";
} else if (averageSales >= 7000) {
return "Good";
} else if (averageSales >= 4000) {
return "Satisfactory";
} else {
return "Needs Improvement";
}
}
-
function determinePerformanceRating(averageSales) {
- This defines a function named
determinePerformanceRating
that takes one argument,averageSales
, representing the average sales for a salesperson.
- This defines a function named
-
if (averageSales > 10000) {
- This
if
statement checks if theaverageSales
is greater than 10,000. If it is, the following block of code executes.
- This
-
return "Excellent";
- If the condition is true (average sales are above 10,000), the function returns the string
"Excellent"
, representing the highest performance rating.
- If the condition is true (average sales are above 10,000), the function returns the string
-
} else if (averageSales >= 7000) {
- This
else if
statement checks if theaverageSales
is between 7,000 and 10,000. If true, the next block of code executes.
- This
-
return "Good";
- If the condition is true (average sales are 7,000 to 10,000), the function returns the string
"Good"
, indicating a solid performance rating.
- If the condition is true (average sales are 7,000 to 10,000), the function returns the string
-
} else if (averageSales >= 4000) {
- This condition checks if the
averageSales
is between 4,000 and 7,000. If so, the next block of code will execute.
- This condition checks if the
-
return "Satisfactory";
- If the condition is true (average sales are 4,000 to 7,000), the function returns the string
"Satisfactory"
, indicating an acceptable performance rating.
- If the condition is true (average sales are 4,000 to 7,000), the function returns the string
-
} else {
- If none of the previous conditions were met, the final
else
block is executed.
- If none of the previous conditions were met, the final
-
return "Needs Improvement";
- If the average sales are below 4,000, the function returns the string
"Needs Improvement"
, representing the lowest performance rating.
- If the average sales are below 4,000, the function returns the string
Code:
function findTopAndBottomPerformers(salesData) {
const sortedData = salesData.sort((a, b) => {
const totalSalesA = a.sales.reduce((sum, sale) => sum + sale, 0);
const totalSalesB = b.sales.reduce((sum, sale) => sum + sale, 0);
return totalSalesB - totalSalesA;
});
const topPerformer = sortedData[0];
const bottomPerformer = sortedData[sortedData.length - 1];
return { topPerformer, bottomPerformer };
}
-
function findTopAndBottomPerformers(salesData) {
- This line defines a function called
findTopAndBottomPerformers
that takes an array of objectssalesData
as input. Each object represents a salesperson and their corresponding sales.
- This line defines a function called
-
const sortedData = salesData.sort((a, b) => {
- The
sort()
method is called on thesalesData
array to sort it by the total sales of each salesperson. a
andb
represent two salesperson objects being compared during the sorting process.
- The
-
const totalSalesA = a.sales.reduce((sum, sale) => sum + sale, 0);
- For each salesperson
a
, thereduce()
method sums up their sales to compute their total sales. The result is stored intotalSalesA
.
- For each salesperson
-
const totalSalesB = b.sales.reduce((sum, sale) => sum + sale, 0);
- Similarly, for salesperson
b
, their total sales are calculated usingreduce()
and stored intotalSalesB
.
- Similarly, for salesperson
-
return totalSalesB - totalSalesA;
- This line returns the difference between
totalSalesB
andtotalSalesA
. IftotalSalesB
is greater, the salespersonb
is placed beforea
in the sorted array (descending order).
- This line returns the difference between
-
const topPerformer = sortedData[0];
- After sorting, the first element in
sortedData
is the salesperson with the highest total sales, stored astopPerformer
.
- After sorting, the first element in
-
const bottomPerformer = sortedData[sortedData.length - 1];
- The last element in
sortedData
is the salesperson with the lowest total sales, stored asbottomPerformer
.
- The last element in
-
return { topPerformer, bottomPerformer };
- The function returns an object with properties
topPerformer
andbottomPerformer
to identify the top and bottom performers.
- The function returns an object with properties
Code:
function generatePerformanceReport(salesData) {
salesData.forEach(person => {
const averageSales = calculateAverageSales(person.sales);
const rating = determinePerformanceRating(averageSales);
console.log(`${person.name}'s average sales: $${averageSales.toFixed(2)} (${rating})`);
});
const { topPerformer, bottomPerformer } = findTopAndBottomPerformers(salesData);
console.log(`Top Performer: ${topPerformer.name} with total sales of $${topPerformer.sales.reduce((sum, sale) => sum + sale, 0)}`);
console.log(`Bottom Performer: ${bottomPerformer.name} with total sales of $${bottomPerformer.sales.reduce((sum, sale) => sum + sale, 0)}`);
}
-
function generatePerformanceReport(salesData) {
- This function,
generatePerformanceReport
, takessalesData
as input. ThesalesData
array contains salesperson objects with names and sales arrays.
- This function,
-
salesData.forEach(person => {
- The
forEach()
method is used to loop through each salesperson (person
) in thesalesData
array.
- The
-
const averageSales = calculateAverageSales(person.sales);
- The function
calculateAverageSales
is called with thesales
array of the current salesperson (person
). The average sales for that person is calculated and stored inaverageSales
.
- The function
-
const rating = determinePerformanceRating(averageSales);
- The function
determinePerformanceRating
is called with theaverageSales
. It returns the performance rating ("Excellent"
,"Good"
, etc.), which is stored inrating
.
- The function
-
console.log(
${person.name}'s average sales: $ ${averageSales.toFixed(2)} (${rating}));
- This line outputs the salesperson's name, average sales (rounded to two decimal places using
.toFixed(2)
), and their performance rating using template literals.
- This line outputs the salesperson's name, average sales (rounded to two decimal places using
-
const { topPerformer, bottomPerformer } = findTopAndBottomPerformers(salesData);
- The
findTopAndBottomPerformers
function is called, which returns an object containing the top and bottom performers. ThetopPerformer
andbottomPerformer
are extracted using object destructuring.
- The
-
console.log(
Top Performer:${topPerformer.name} with total sales of $ ${topPerformer.sales.reduce((sum, sale) => sum + sale, 0)});
- This line outputs the name and total sales of the top performer. The total sales are calculated
by summing their sales using reduce()
.
-
console.log(
Bottom Performer:${bottomPerformer.name} with total sales of $ ${bottomPerformer.sales.reduce((sum, sale) => sum + sale, 0)});
- Similarly, this line outputs the name and total sales of the bottom performer.
Code:
const salesData = [
{ name: 'Alice', sales: [12000, 15000, 13000] },
{ name: 'Bob', sales: [7000, 6000, 7500] },
{ name: 'Charlie', sales: [3000, 4000, 3500] },
{ name: 'Diana', sales: [9000, 8500, 9200] }
];
generatePerformanceReport(salesData);
Expected Output:
Alice's average sales: $13333.33 (Excellent)
Bob's average sales: $6833.33 (Satisfactory)
Charlie's average sales: $3500.00 (Needs Improvement)
Diana's average sales: $8900.00 (Good)
Top Performer: Alice with total sales of $40000
Bottom Performer: Charlie with total sales of $10500
- The solution uses modular functions to break down each task, making the code maintainable and reusable.
- Template literals make the output more readable by embedding variables directly in strings.
- The use of array methods like
reduce()
,sort()
, andforEach()
simplifies operations on the data. - The code handles real-world data processing scenarios like calculating averages, determining ratings, and identifying top and bottom performers.