Utmaning: Lägg till metoder i en klass
Uppgift
Du arbetar med en Book-klass som representerar böcker i ett bibliotek. Varje bok har en titel, författare och genre. Din uppgift är att lägga till metoder i denna klass för att hämta information och uppdatera genren.
- Slutför metoddefinitionerna:
- I den befintliga
Book-klassen, lägg till en metod som hetergetInfosom returnerar en sträng i formatet:"Title by Author is a Genre book."; - Lägg till en annan metod med namnet
updateGenresom tar en parameter,newGenre, och uppdaterar bokensgenre-egenskap.
- I den befintliga
- Testa metoderna:
- En instans av
Bookmed namnetbook1har redan skapats med värdena"The Great Gatsby","F. Scott Fitzgerald"och"Classic"; - Anropa
getInfoför att logga information om boken; - Använd
updateGenreför att ändra genren till"Historical Fiction"; - Anropa
getInfoigen för att bekräfta genreuppdateringen.
- En instans av
1234567891011121314151617181920212223class Book { constructor(title, author, genre) { this.title = title; this.author = author; this.genre = genre; } _____() { return `${this._____} by ${this._____} is a ${this._____} book.`; } _____(_____) { this._____ = _____; } } // Instance const book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 'Classic'); // Test the methods console.log(book1._____()); // Expected: The Great Gatsby by F. Scott Fitzgerald is a Classic book. book1._____(_____); // Update genre console.log(book1._____()); // Expected: The Great Gatsby by F. Scott Fitzgerald is a Historical Fiction book.
- Definiera en metod med namnet
getInfoiBook-klassen; - I metoden
getInfo, returnera en sträng som använderthis.title,this.authorochthis.genre; - Definiera en metod med namnet
updateGenresom tar en parameter,newGenre; - I metoden
updateGenre, sättthis.genretillnewGenre; - Anropa
getInfopåbook1för att logga den initiala informationen om boken; - Använd
updateGenrepåbook1för att ändra genren till"Historical Fiction"; - Anropa
getInfopåbook1igen för att bekräfta den uppdaterade genren.
1234567891011121314151617181920212223class Book { constructor(title, author, genre) { this.title = title; this.author = author; this.genre = genre; } getInfo() { return `${this.title} by ${this.author} is a ${this.genre} book.`; } updateGenre(newGenre) { this.genre = newGenre; } } // Instance const book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 'Classic'); // Test the methods console.log(book1.getInfo()); // Output: The Great Gatsby by F. Scott Fitzgerald is a Classic book. book1.updateGenre('Historical Fiction'); // Update genre console.log(book1.getInfo()); // Output: The Great Gatsby by F. Scott Fitzgerald is a Historical Fiction book.
Tack för dina kommentarer!
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
Can you explain how the getInfo method works?
What happens if I update the genre to something else?
Can I add more properties or methods to the Book class?
Awesome!
Completion rate improved to 2.22
Utmaning: Lägg till metoder i en klass
Svep för att visa menyn
Uppgift
Du arbetar med en Book-klass som representerar böcker i ett bibliotek. Varje bok har en titel, författare och genre. Din uppgift är att lägga till metoder i denna klass för att hämta information och uppdatera genren.
- Slutför metoddefinitionerna:
- I den befintliga
Book-klassen, lägg till en metod som hetergetInfosom returnerar en sträng i formatet:"Title by Author is a Genre book."; - Lägg till en annan metod med namnet
updateGenresom tar en parameter,newGenre, och uppdaterar bokensgenre-egenskap.
- I den befintliga
- Testa metoderna:
- En instans av
Bookmed namnetbook1har redan skapats med värdena"The Great Gatsby","F. Scott Fitzgerald"och"Classic"; - Anropa
getInfoför att logga information om boken; - Använd
updateGenreför att ändra genren till"Historical Fiction"; - Anropa
getInfoigen för att bekräfta genreuppdateringen.
- En instans av
1234567891011121314151617181920212223class Book { constructor(title, author, genre) { this.title = title; this.author = author; this.genre = genre; } _____() { return `${this._____} by ${this._____} is a ${this._____} book.`; } _____(_____) { this._____ = _____; } } // Instance const book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 'Classic'); // Test the methods console.log(book1._____()); // Expected: The Great Gatsby by F. Scott Fitzgerald is a Classic book. book1._____(_____); // Update genre console.log(book1._____()); // Expected: The Great Gatsby by F. Scott Fitzgerald is a Historical Fiction book.
- Definiera en metod med namnet
getInfoiBook-klassen; - I metoden
getInfo, returnera en sträng som använderthis.title,this.authorochthis.genre; - Definiera en metod med namnet
updateGenresom tar en parameter,newGenre; - I metoden
updateGenre, sättthis.genretillnewGenre; - Anropa
getInfopåbook1för att logga den initiala informationen om boken; - Använd
updateGenrepåbook1för att ändra genren till"Historical Fiction"; - Anropa
getInfopåbook1igen för att bekräfta den uppdaterade genren.
1234567891011121314151617181920212223class Book { constructor(title, author, genre) { this.title = title; this.author = author; this.genre = genre; } getInfo() { return `${this.title} by ${this.author} is a ${this.genre} book.`; } updateGenre(newGenre) { this.genre = newGenre; } } // Instance const book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 'Classic'); // Test the methods console.log(book1.getInfo()); // Output: The Great Gatsby by F. Scott Fitzgerald is a Classic book. book1.updateGenre('Historical Fiction'); // Update genre console.log(book1.getInfo()); // Output: The Great Gatsby by F. Scott Fitzgerald is a Historical Fiction book.
Tack för dina kommentarer!