Challenge: Add Methods to a Class
Task
You're working with a Book class that represents books in a library. Each book has a title, author, and genre. Your task is to add methods to this class to retrieve information and update the genre.
- Complete the Method Definitions:
- In the existing
Bookclass, add a method calledgetInfothat returns a string in the format:"Title by Author is a Genre book."; - Add another method named
updateGenrethat takes one parameter,newGenre, and updates the book'sgenreproperty.
- In the existing
- Test the Methods:
- An instance of
Booknamedbook1has already been created with the values"The Great Gatsby"," F. Scott Fitzgerald", and"Classic"; - Call
getInfoto log information about the book; - Use
updateGenreto change the genre to"Historical Fiction"; - Call
getInfoagain to confirm the genre update.
- An instance of
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.
- Define a method named
getInfoin theBookclass; - In the
getInfomethod, return a string that usesthis.title,this.author, andthis.genre; - Define a method named
updateGenrethat takes one parameter,newGenre; - In the
updateGenremethod, setthis.genretonewGenre; - Call
getInfoonbook1to log the initial information about the book; - Use
updateGenreonbook1to change the genre to"Historical Fiction"; - Call
getInfoonbook1again to confirm the updated genre.
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.
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 5
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Suggested prompts:
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
Challenge: Add Methods to a Class
Swipe to show menu
Task
You're working with a Book class that represents books in a library. Each book has a title, author, and genre. Your task is to add methods to this class to retrieve information and update the genre.
- Complete the Method Definitions:
- In the existing
Bookclass, add a method calledgetInfothat returns a string in the format:"Title by Author is a Genre book."; - Add another method named
updateGenrethat takes one parameter,newGenre, and updates the book'sgenreproperty.
- In the existing
- Test the Methods:
- An instance of
Booknamedbook1has already been created with the values"The Great Gatsby"," F. Scott Fitzgerald", and"Classic"; - Call
getInfoto log information about the book; - Use
updateGenreto change the genre to"Historical Fiction"; - Call
getInfoagain to confirm the genre update.
- An instance of
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.
- Define a method named
getInfoin theBookclass; - In the
getInfomethod, return a string that usesthis.title,this.author, andthis.genre; - Define a method named
updateGenrethat takes one parameter,newGenre; - In the
updateGenremethod, setthis.genretonewGenre; - Call
getInfoonbook1to log the initial information about the book; - Use
updateGenreonbook1to change the genre to"Historical Fiction"; - Call
getInfoonbook1again to confirm the updated genre.
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.
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 5