0
0
Javascriptprogramming~15 mins

Operator precedence in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Operator precedence
📖 Scenario: Imagine you are calculating the total cost of items in a shopping cart with discounts and taxes. You need to understand how JavaScript calculates expressions with multiple operators to get the correct total.
🎯 Goal: You will write a JavaScript program that calculates the final price of an item using operator precedence rules. You will create variables for price, discount, and tax rate, then write an expression that applies discount and tax correctly.
📋 What You'll Learn
Create variables for price, discount, and taxRate with exact values
Create a variable discountedPrice that applies the discount to the price
Create a variable finalPrice that adds tax to the discounted price using correct operator precedence
Print the finalPrice with two decimal places
💡 Why This Matters
🌍 Real World
Calculating prices with discounts and taxes is common in shopping apps and websites.
💼 Career
Understanding operator precedence helps avoid bugs in financial calculations and improves coding accuracy.
Progress0 / 4 steps
1
Set up the initial price, discount, and tax rate
Create three variables: price with value 100, discount with value 0.2 (20%), and taxRate with value 0.1 (10%).
Javascript
Need a hint?

Use const to create variables with the exact names and values.

2
Calculate the discounted price
Create a variable called discountedPrice that calculates the price after subtracting the discount. Use the expression price - price * discount.
Javascript
Need a hint?

Remember multiplication happens before subtraction.

3
Calculate the final price including tax
Create a variable called finalPrice that adds tax to the discounted price. Use the expression discountedPrice + discountedPrice * taxRate to apply tax correctly.
Javascript
Need a hint?

Multiplication happens before addition, so tax is calculated on discountedPrice first.

4
Print the final price with two decimals
Use console.log to print finalPrice rounded to two decimal places using finalPrice.toFixed(2).
Javascript
Need a hint?

Use console.log(finalPrice.toFixed(2)) to show two decimals.