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
course content

Зміст курсу

Introduction to TypeScript

Introduction to TypeScript

1. TypeScript Fundamentals
2. Conditional Statements
3. Arrays
4. Loops
5. Functions

book
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!

1234567891011121314
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)
copy

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 6
We're sorry to hear that something went wrong. What happened?
some-alt