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 alt klart?
Tak for dine kommentarer!
Sektion 2. Kapitel 11
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 2.27
Challenge: Work with Object Methods
Stryg for at vise menuen
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 alt klart?
Tak for dine kommentarer!
Sektion 2. Kapitel 11