Desafío: Agregar Métodos a una Clase
Tarea
Estás trabajando con una clase Book que representa libros en una biblioteca. Cada libro tiene un título, autor y género. Tu tarea es agregar métodos a esta clase para recuperar información y actualizar el género.
- Completa las definiciones de los métodos:
- En la clase
Bookexistente, agrega un método llamadogetInfoque devuelva una cadena con el formato:"Title by Author is a Genre book."; - Agrega otro método llamado
updateGenreque reciba un parámetro,newGenre, y actualice la propiedadgenredel libro.
- En la clase
- Prueba los métodos:
- Ya se ha creado una instancia de
Bookllamadabook1con los valores"The Great Gatsby"," F. Scott Fitzgerald"y"Classic"; - Llama a
getInfopara mostrar información sobre el libro; - Usa
updateGenrepara cambiar el género a"Historical Fiction"; - Llama nuevamente a
getInfopara confirmar la actualización del género.
- Ya se ha creado una instancia de
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.
- Definir un método llamado
getInfoen la claseBook; - En el método
getInfo, devolver una cadena que utilicethis.title,this.authorythis.genre; - Definir un método llamado
updateGenreque reciba un parámetro,newGenre; - En el método
updateGenre, asignarthis.genreanewGenre; - Llamar a
getInfoenbook1para mostrar la información inicial del libro; - Usar
updateGenreenbook1para cambiar el género a"Historical Fiction"; - Llamar nuevamente a
getInfoenbook1para confirmar el género actualizado.
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.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
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
Desafío: Agregar Métodos a una Clase
Desliza para mostrar el menú
Tarea
Estás trabajando con una clase Book que representa libros en una biblioteca. Cada libro tiene un título, autor y género. Tu tarea es agregar métodos a esta clase para recuperar información y actualizar el género.
- Completa las definiciones de los métodos:
- En la clase
Bookexistente, agrega un método llamadogetInfoque devuelva una cadena con el formato:"Title by Author is a Genre book."; - Agrega otro método llamado
updateGenreque reciba un parámetro,newGenre, y actualice la propiedadgenredel libro.
- En la clase
- Prueba los métodos:
- Ya se ha creado una instancia de
Bookllamadabook1con los valores"The Great Gatsby"," F. Scott Fitzgerald"y"Classic"; - Llama a
getInfopara mostrar información sobre el libro; - Usa
updateGenrepara cambiar el género a"Historical Fiction"; - Llama nuevamente a
getInfopara confirmar la actualización del género.
- Ya se ha creado una instancia de
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.
- Definir un método llamado
getInfoen la claseBook; - En el método
getInfo, devolver una cadena que utilicethis.title,this.authorythis.genre; - Definir un método llamado
updateGenreque reciba un parámetro,newGenre; - En el método
updateGenre, asignarthis.genreanewGenre; - Llamar a
getInfoenbook1para mostrar la información inicial del libro; - Usar
updateGenreenbook1para cambiar el género a"Historical Fiction"; - Llamar nuevamente a
getInfoenbook1para confirmar el género actualizado.
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.
¡Gracias por tus comentarios!