Skip to content

Latest commit

 

History

History
204 lines (149 loc) · 9.45 KB

02_coding_challenge_3_answer_key.md

File metadata and controls

204 lines (149 loc) · 9.45 KB

Here's an extended discussion of the code for the Sales Performance Analyzer, explaining each line in detail:


1. Calculate Average Sales

Code:

function calculateAverageSales(sales) {
    const totalSales = sales.reduce((sum, sale) => sum + sale, 0);
    return totalSales / sales.length;
}

Line-by-Line Discussion:

  1. 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.
  2. const totalSales = sales.reduce((sum, sale) => sum + sale, 0);

    • This line declares a constant variable totalSales and uses the reduce() method to sum all the values in the sales 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 where sum holds the running total, and sale is the current element in the array. The function returns the sum of all elements in the array.
    • The second argument to reduce() is 0, which initializes sum to 0 for the first iteration.
  3. 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.

2. Determine Performance Rating

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";
    }
}

Line-by-Line Discussion:

  1. function determinePerformanceRating(averageSales) {

    • This defines a function named determinePerformanceRating that takes one argument, averageSales, representing the average sales for a salesperson.
  2. if (averageSales > 10000) {

    • This if statement checks if the averageSales is greater than 10,000. If it is, the following block of code executes.
  3. return "Excellent";

    • If the condition is true (average sales are above 10,000), the function returns the string "Excellent", representing the highest performance rating.
  4. } else if (averageSales >= 7000) {

    • This else if statement checks if the averageSales is between 7,000 and 10,000. If true, the next block of code executes.
  5. 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.
  6. } 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.
  7. 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.
  8. } else {

    • If none of the previous conditions were met, the final else block is executed.
  9. return "Needs Improvement";

    • If the average sales are below 4,000, the function returns the string "Needs Improvement", representing the lowest performance rating.

3. Find Top and Bottom Performers

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 };
}

Line-by-Line Discussion:

  1. function findTopAndBottomPerformers(salesData) {

    • This line defines a function called findTopAndBottomPerformers that takes an array of objects salesData as input. Each object represents a salesperson and their corresponding sales.
  2. const sortedData = salesData.sort((a, b) => {

    • The sort() method is called on the salesData array to sort it by the total sales of each salesperson.
    • a and b represent two salesperson objects being compared during the sorting process.
  3. const totalSalesA = a.sales.reduce((sum, sale) => sum + sale, 0);

    • For each salesperson a, the reduce() method sums up their sales to compute their total sales. The result is stored in totalSalesA.
  4. const totalSalesB = b.sales.reduce((sum, sale) => sum + sale, 0);

    • Similarly, for salesperson b, their total sales are calculated using reduce() and stored in totalSalesB.
  5. return totalSalesB - totalSalesA;

    • This line returns the difference between totalSalesB and totalSalesA. If totalSalesB is greater, the salesperson b is placed before a in the sorted array (descending order).
  6. const topPerformer = sortedData[0];

    • After sorting, the first element in sortedData is the salesperson with the highest total sales, stored as topPerformer.
  7. const bottomPerformer = sortedData[sortedData.length - 1];

    • The last element in sortedData is the salesperson with the lowest total sales, stored as bottomPerformer.
  8. return { topPerformer, bottomPerformer };

    • The function returns an object with properties topPerformer and bottomPerformer to identify the top and bottom performers.

4. Generate Performance Report

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)}`);
}

Line-by-Line Discussion:

  1. function generatePerformanceReport(salesData) {

    • This function, generatePerformanceReport, takes salesData as input. The salesData array contains salesperson objects with names and sales arrays.
  2. salesData.forEach(person => {

    • The forEach() method is used to loop through each salesperson (person) in the salesData array.
  3. const averageSales = calculateAverageSales(person.sales);

    • The function calculateAverageSales is called with the sales array of the current salesperson (person). The average sales for that person is calculated and stored in averageSales.
  4. const rating = determinePerformanceRating(averageSales);

    • The function determinePerformanceRating is called with the averageSales. It returns the performance rating ("Excellent", "Good", etc.), which is stored in rating.
  5. 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.
  6. const { topPerformer, bottomPerformer } = findTopAndBottomPerformers(salesData);

    • The findTopAndBottomPerformers function is called, which returns an object containing the top and bottom performers. The topPerformer and bottomPerformer are extracted using object destructuring.
  7. 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().

  1. 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.

Testing the Functions with Sample Data

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

Summary

  • 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(), and forEach() simplifies operations on the data.
  • The code handles real-world data processing scenarios like calculating averages, determining ratings, and identifying top and bottom performers.