Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Working with Object Methods | Objects Fundamentals
JavaScript Data Structures
course content

Course Content

JavaScript Data Structures

JavaScript Data Structures

1. Introduction and Prerequisites
2. Objects Fundamentals
3. Advanced Object Manipulation
4. Mastering Arrays
5. Advanced Array Operations

book
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 the this keyword to access the car's basePrice.
  • Use the this keyword to access the options (leatherSeats, sunroof, and navigationSystem) from the options 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.
12345678910111213141516
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();
copy

Expected output:

  1. Inside the method, use this.basePrice to access the base price.
  2. Use this.options to access the options object.
  3. You can use the dot notation to access option properties (e.g., this.options.leatherSeats).
1234567891011121314151617181920
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();
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 11
We're sorry to hear that something went wrong. What happened?
some-alt