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();
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 2. Kapitel 11
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 2.27
Challenge: Work with Object Methods
Svep för att visa menyn
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();
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 2. Kapitel 11