Let's Go Shopping!
Welcome to our interactive tutorial on Arithmetic Operations in JavaScript!
In this tutorial, we'll learn how to perform basic arithmetic operations in JavaScript while calculating the total price of two items in a shopping cart.
Your Task:
You have two items in your shopping cart: a T-shirt worth $15 and a pair of jeans worth $30. Calculate the total price of these items using JavaScript.
Step 1: Create Variables
Open your index.js
file and create two variables to store the prices of the T-shirt and jeans:
let tShirtPrice = 15let jeansPrice = 30
Run your code to ensure it's working correctly. You shouldn't see any output yet, but that's okay!
Step 2: Add the Prices
Now, let's add the prices of the two items together. We'll use the +
operator to perform addition. Add the following line of code:
let totalPrice = tShirtPrice + jeansPrice
Run your code again to see the result.
Step 3: Display the Total Price
Use a template literal to display the total price. Add the following line of code:
console.log(`The total price is $${totalPrice}`)
Run your code one more time to see the final output.
What to Expect:
When you run your code, you should see the following output in the console:
The total price is $45
Congratulations! You've successfully calculated the total price of the two items in your shopping cart using arithmetic operations in JavaScript!
Recap:
In this tutorial, we learned how to:
+
operator to add prices together.You're now ready to move on to the next tutorial, where you'll learn about Boolean Logic!
Happy Coding!
Tests