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
calculateTotalPricemethod, use thethiskeyword to access the car'sbasePrice. - Use the
thiskeyword to access the options (leatherSeats,sunroof, andnavigationSystem) from theoptionsobject. - 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();
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the total price is calculated in the method?
What does the `this` keyword refer to inside the `calculateTotalPrice` method?
Can you show how to add more options to the car object?
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
calculateTotalPricemethod, use thethiskeyword to access the car'sbasePrice. - Use the
thiskeyword to access the options (leatherSeats,sunroof, andnavigationSystem) from theoptionsobject. - 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();
Thanks for your feedback!