Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Adding Methods to a Class | Classes
Advanced JavaScript Mastery
course content

Contenido del Curso

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

bookChallenge: Adding 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.

  1. Complete the Method Definitions:
    • In the existing Book class, add a method called getInfo that returns a string in the format: "Title by Author is a Genre book.";
    • Add another method named updateGenre that takes one parameter, newGenre, and updates the book's genre property.
  2. Test the Methods:
    • An instance of Book named book1 has already been created with the values "The Great Gatsby", " F. Scott Fitzgerald", and "Classic";
    • Call getInfo to log information about the book;
    • Use updateGenre to change the genre to "Historical Fiction";
    • Call getInfo again to confirm the genre update.
1234567891011121314151617181920212223
class 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.
copy
  • Define a method named getInfo in the Book class;
  • In the getInfo method, return a string that uses this.title, this.author, and this.genre;
  • Define a method named updateGenre that takes one parameter, newGenre;
  • In the updateGenre method, set this.genre to newGenre;
  • Call getInfo on book1 to log the initial information about the book;
  • Use updateGenre on book1 to change the genre to "Historical Fiction";
  • Call getInfo on book1 again to confirm the updated genre.
1234567891011121314151617181920212223
class 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.
copy

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt