Junior — Senior
Calculation of the total cost of goods including discounts
livecode
Task condition
Given an array of objects products. You need to implement a function calculateTotal that takes an array of products and a discount object discountPercentage. The function should calculate the total cost of all products without discounts, as well as the cost with discounts applied, and return an object result, whose structure is demonstrated in the comments in the code. The solution should be written in TypeScript.
const products = [
{ id: 1123, price: 20, name: 'apple', color: 'red', category: 'fruit' },
{ id: 4322, price: 20, name: 'apple', ferm: 'siberia', category: 'vegetable' },
];
function calculateTotal(products, discount) {}
const discountPercentage = { fruit: 10, vegetable: 30 };
const result = calculateTotal(products, discountPercentage);
console.log("receipt", result);
// "receipt", {
// "totalWithoutDiscount": 40,
// "totalWithDiscount": 32,
// "discount": {
// "fruit": 10,
// "vegetable": 30
// },
// "items": [
// {
// "id": 1123,
// "price": 20,
// "name": "apple",
// "color": "red",
// "category": "fruit"
// },
// {
// "id": 4322,
// "price": 20,
// "name": "apple",
// "ferm": "siberia",
// "category": "vegetable"
// }
// ]
// }