Course Content
JavaScript Data Structures
JavaScript Data Structures
Challenge: Working with Object Methods
Task
You're provided with an object representing a car's details. Your task is to create a method within the object that calculates the car's total price. The car's total cost is calculated by adding the base price and the sum of additional options.
- Inside the
calculateTotalPrice
method, use thethis
keyword to access the car'sbasePrice
. - Use the
this
keyword to access the options (leatherSeats
,sunroof
, andnavigationSystem
) from theoptions
object. - Calculate the total price by adding the base price and the sum of all options.
- Log the total price as the method's result.
const car = { make: "Ford", model: "F-150", basePrice: 72000, options: { leatherSeats: 2400, sunroof: 100, navigationSystem: 1650, }, calculateTotalPrice() { const totalPrice = ___ ; console.log("Total price is", totalPrice); }, }; car.calculateTotalPrice();
Expected output:
- Inside the method, use
this.basePrice
to access the base price. - Use
this.options
to access the options object. - You can use the dot notation to access option properties (e.g.,
this.options.leatherSeats
).
const car = { make: "Ford", model: "F-150", basePrice: 72000, options: { leatherSeats: 2400, sunroof: 100, navigationSystem: 1650, }, calculateTotalPrice() { const totalPrice = this.basePrice + this.options.leatherSeats + this.options.sunroof + this.options.navigationSystem; console.log("Total price is", totalPrice); }, }; car.calculateTotalPrice();
Thanks for your feedback!