Зміст курсу
Introduction to TypeScript
Introduction to TypeScript
Challenge (price calculator)
Task
If you were able to understand the last code from the previous chapter, this task should not be difficult for you.
Your task is to calculate the final price of the order, taking into account the price
of one unit of the product, the quantity
of the ordered product, and the discount
. Note that the quantity
of the product should have a default value. If the quantity
of the product is not specified, we assume that the buyer is purchasing one unit of the product. The discount
is an optional parameter because it is not always available, and the product can be purchased without a discount.
You need to fill in the gaps(___
) in the code below so that it checks whether the discount parameter is specified and correctly calculates the final order price.
Use the hint and solution buttons if you find the task difficult. You can always analyze the solution, and it will stick in your memory. Remember, we are all just learning, and success awaits you ahead!
function calculatePrice(price: number, quantity: number, discount: number): number { let priceWithoutDiscount = (___ * ___); let result; if (___) { result = ___ } else { result = ___; } return result; } console.log(calculatePrice(20)); // Expected result: 20 (no discount, one item) console.log(calculatePrice(15, 3)); // Expected result: 45 (no discount, three items) console.log(calculatePrice(50, 2, 10)); // Expected result: 90 (10% discount applied, two items)
Дякуємо за ваш відгук!