Udfordring: Arbejde med Objektmetoder
Opgave
Du får et objekt, der repræsenterer en bils detaljer. Din opgave er at oprette en metode i objektet, der beregner bilens samlede pris. Bilens samlede pris beregnes ved at lægge grundprisen sammen med summen af ekstraudstyr.
- Inden for
calculateTotalPrice-metoden skal du brugethis-nøglen til at få adgang til bilensbasePrice. - Brug
this-nøglen til at få adgang til mulighederne (leatherSeats,sunroofognavigationSystem) fraoptions-objektet. - Beregn den samlede pris ved at lægge grundprisen og summen af alle muligheder sammen.
- Log den samlede pris som metodens resultat.
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();
Forventet output:
Total price is 76150
- Inde i metoden bruges
this.basePricetil at tilgå basisprisen. - Brug
this.optionstil at tilgå options-objektet. - Dot-notation kan anvendes til at tilgå egenskaber for options (f.eks.
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();
Tak for dine kommentarer!
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
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
Udfordring: Arbejde med Objektmetoder
Stryg for at vise menuen
Opgave
Du får et objekt, der repræsenterer en bils detaljer. Din opgave er at oprette en metode i objektet, der beregner bilens samlede pris. Bilens samlede pris beregnes ved at lægge grundprisen sammen med summen af ekstraudstyr.
- Inden for
calculateTotalPrice-metoden skal du brugethis-nøglen til at få adgang til bilensbasePrice. - Brug
this-nøglen til at få adgang til mulighederne (leatherSeats,sunroofognavigationSystem) fraoptions-objektet. - Beregn den samlede pris ved at lægge grundprisen og summen af alle muligheder sammen.
- Log den samlede pris som metodens resultat.
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();
Forventet output:
Total price is 76150
- Inde i metoden bruges
this.basePricetil at tilgå basisprisen. - Brug
this.optionstil at tilgå options-objektet. - Dot-notation kan anvendes til at tilgå egenskaber for options (f.eks.
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();
Tak for dine kommentarer!