Utfordring: Arbeid med Objektmetoder
Oppgave
Du får et objekt som representerer detaljene til en bil. Din oppgave er å lage en metode i objektet som beregner bilens totalpris. Bilens totale kostnad beregnes ved å legge sammen grunnprisen og summen av tilleggsutstyr.
- Inne i metoden
calculateTotalPrice, bruk nøkkelordetthisfor å få tilgang til bilensbasePrice. - Bruk nøkkelordet
thisfor å få tilgang til tilvalgene (leatherSeats,sunroofognavigationSystem) fraoptions-objektet. - Beregn totalprisen ved å legge sammen grunnprisen og summen av alle tilvalg.
- Logg totalprisen som resultatet av metoden.
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 utdata:
Total price is 76150
- Inne i metoden, bruk
this.basePricefor å få tilgang til grunnprisen. - Bruk
this.optionsfor å få tilgang til options-objektet. - Du kan bruke punktnotasjon for å få tilgang til egenskaper i 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();
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
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
Utfordring: Arbeid med Objektmetoder
Sveip for å vise menyen
Oppgave
Du får et objekt som representerer detaljene til en bil. Din oppgave er å lage en metode i objektet som beregner bilens totalpris. Bilens totale kostnad beregnes ved å legge sammen grunnprisen og summen av tilleggsutstyr.
- Inne i metoden
calculateTotalPrice, bruk nøkkelordetthisfor å få tilgang til bilensbasePrice. - Bruk nøkkelordet
thisfor å få tilgang til tilvalgene (leatherSeats,sunroofognavigationSystem) fraoptions-objektet. - Beregn totalprisen ved å legge sammen grunnprisen og summen av alle tilvalg.
- Logg totalprisen som resultatet av metoden.
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 utdata:
Total price is 76150
- Inne i metoden, bruk
this.basePricefor å få tilgang til grunnprisen. - Bruk
this.optionsfor å få tilgang til options-objektet. - Du kan bruke punktnotasjon for å få tilgang til egenskaper i 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();
Takk for tilbakemeldingene dine!