Test Assignment: Order Price Calculation with Discounts and Taxes

Description

Your team is developing an e-commerce platform. Your task is to implement a function that calculates the final price of an order, considering possible discounts and taxes.

Technical Requirements

Implement a function calculateTotal(order), which takes an order object as input and returns a number representing the final order price.

Important Rules

P.S. Your task code gonna be tested automatically, and compiled ONLY with JS, so please, provide your code ONLY in JS

Input Structure

The input is an object with the following structure:

{
  items: [  // array of items in the order
    { price: 100, quantity: 2 }, // item costing 100, quantity 2
    { price: 200, quantity: 1 }
  ],
  discount: { type: "percentage", value: 10 }, // 10% discount
  tax: 7 // 7% tax (applied after discount)
}

Supported Discount Types

Tax (tax) is applied after all discounts.

Additional Conditions

Expected Output

The function must return the final order total as a number.

Example Call

order = {
    items: [
      { price: 100, quantity: 2 },
      { price: 200, quantity: 1 }
    ],
    discount: { type: "percentage", value: 10 },
    tax: 7
  }
calculateTotal(order);
// Expected result: 385.20

Calculation Steps

  1. Calculate the initial total (sum of price × quantity for all items)
  2. Apply discount if present:
  3. Apply tax to the discounted amount
  4. Round the final result to 2 decimal places (if needed)

Evaluation Criteria

Submission Guidelines

  1. Implement only the calculateTotal(order) function
  2. Submit your solution in a .txt text file
  3. Name the file using the following format: your_name.txt
  4. Our team will run predefined tests against your function
  5. Ensure the function correctly passes all test cases

Example Solution Template

function calculateTotal(order) {
  // Your implementation here
}

Good luck!