Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Utfordring: Arbeid med Objektmetoder | Grunnleggende om JavaScript-Objekter
Javascript Datastrukturer

bookUtfordring: 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økkelordet this for å få tilgang til bilens basePrice.
  • Bruk nøkkelordet this for å få tilgang til tilvalgene (leatherSeats, sunroof og navigationSystem) fra options-objektet.
  • Beregn totalprisen ved å legge sammen grunnprisen og summen av alle tilvalg.
  • Logg totalprisen som resultatet av metoden.
12345678910111213141516
const 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();
copy

Forventet utdata:

Total price is 76150
  1. Inne i metoden, bruk this.basePrice for å få tilgang til grunnprisen.
  2. Bruk this.options for å få tilgang til options-objektet.
  3. Du kan bruke punktnotasjon for å få tilgang til egenskaper i options (f.eks. this.options.leatherSeats).
1234567891011121314151617181920
const 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();
copy

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 11

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

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

bookUtfordring: 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økkelordet this for å få tilgang til bilens basePrice.
  • Bruk nøkkelordet this for å få tilgang til tilvalgene (leatherSeats, sunroof og navigationSystem) fra options-objektet.
  • Beregn totalprisen ved å legge sammen grunnprisen og summen av alle tilvalg.
  • Logg totalprisen som resultatet av metoden.
12345678910111213141516
const 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();
copy

Forventet utdata:

Total price is 76150
  1. Inne i metoden, bruk this.basePrice for å få tilgang til grunnprisen.
  2. Bruk this.options for å få tilgang til options-objektet.
  3. Du kan bruke punktnotasjon for å få tilgang til egenskaper i options (f.eks. this.options.leatherSeats).
1234567891011121314151617181920
const 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();
copy

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 11
some-alt