Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge (price calculator) | Functions
Introduction to TypeScript

Challenge (price calculator) 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!

1. Your task is to calculate the user's order cost, for this purpose, the variables priceWithoutDiscount and result have been created.
2. Initialize this variable as the price multiplied by the quantity.
3. Next, depending on whether you have a discount, you need to calculate the final price.
4. If there is a discount, you should subtract it, which can be done like this: priceWithoutDiscount - priceWithoutDiscount * (discount / 100).
5. If there is no discount, then the price without a discount will be the result.
6. Note that quantity should be a default parameter, and discount is optional.
7. The default value for quantity is 1.

Все було зрозуміло?

Секція 5. Розділ 6
course content

Зміст курсу

Introduction to TypeScript

Challenge (price calculator) 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!

1. Your task is to calculate the user's order cost, for this purpose, the variables priceWithoutDiscount and result have been created.
2. Initialize this variable as the price multiplied by the quantity.
3. Next, depending on whether you have a discount, you need to calculate the final price.
4. If there is a discount, you should subtract it, which can be done like this: priceWithoutDiscount - priceWithoutDiscount * (discount / 100).
5. If there is no discount, then the price without a discount will be the result.
6. Note that quantity should be a default parameter, and discount is optional.
7. The default value for quantity is 1.

Все було зрозуміло?

Секція 5. Розділ 6
some-alt