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
- The assignment and the provided code must be written in pure JavaScript (no external libraries).
- The code must not throw
throw Error
under any circumstances.
- If any input values are invalid, they should be treated as
0
.
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
"fixed"
— a fixed discount amount, e.g., { type: "fixed", value: 50 }
"percentage"
— a percentage-based discount, e.g., { type: "percentage", value: 10 }
(10% off the total amount)
Tax (tax
) is applied after all discounts.
Additional Conditions
- The input data may be incorrect (negative prices, invalid discount values)
- The function should handle edge cases correctly
- The final result should be rounded to two decimal places
- You must not modify parameter names or the structure of the input object
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
- Calculate the initial total (sum of price × quantity for all items)
- Apply discount if present:
- For fixed discount: subtract the discount amount
- For percentage discount: subtract the percentage from the total
- Apply tax to the discounted amount
- Round the final result to 2 decimal places (if needed)
Evaluation Criteria
- Understanding Requirements: How well discounts and taxes are handled
- Code Quality: Readability, cleanliness, and adherence to JavaScript best practices
- Edge Cases Handling: Processing of invalid and edge-case inputs
Submission Guidelines
- Implement only the
calculateTotal(order)
function
- Submit your solution in a .txt text file
- Name the file using the following format:
your_name.txt
- Our team will run predefined tests against your function
- Ensure the function correctly passes all test cases
Example Solution Template
function calculateTotal(order) {
// Your implementation here
}
Good luck!