Challenge: Work 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.
12345678910111213141516const 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:
Total price is 76150
- 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
).
1234567891011121314151617181920const 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();
Everything was clear?
Thanks for your feedback!
SectionΒ 2. ChapterΒ 11
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.27
Challenge: Work with Object Methods
Swipe to show menu
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.
12345678910111213141516const 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:
Total price is 76150
- 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
).
1234567891011121314151617181920const 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();
Everything was clear?
Thanks for your feedback!
SectionΒ 2. ChapterΒ 11