Skip to content

Instantly share code, notes, and snippets.

@dsibinski
Last active April 18, 2021 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsibinski/38b88a48d8a5a715a3745bc4e6954c3c to your computer and use it in GitHub Desktop.
Save dsibinski/38b88a48d8a5a715a3745bc4e6954c3c to your computer and use it in GitHub Desktop.
export class DiscountApplier {
getDiscountPercentageValue(products: Product[]): number {
let discountValue = 0;
if (products.length === 1) {
return 0;
}
let allProductsQuantity = products.reduce((sum, product) => sum + product.quantity, 0);
if (allProductsQuantity > 10 && allProductsQuantity <= 50) {
discountValue += 10;
}
if (allProductsQuantity > 50) {
discountValue += 15;
}
let allProductsPrice = products.reduce((sum, product) => sum + product.quantity * product.price, 0);
if (allProductsPrice > 100 && allProductsPrice <= 500) {
discountValue += 5;
}
if (allProductsPrice > 500 && allProductsPrice <= 2000) {
discountValue += 10;
}
if (allProductsPrice > 2000) {
discountValue += 25;
}
return discountValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment