Sfida: Aggiungi Metodi a una Classe
Compito
Stai lavorando con una classe Book che rappresenta i libri in una biblioteca. Ogni libro ha un titolo, un autore e un genere. Il tuo compito è aggiungere metodi a questa classe per recuperare informazioni e aggiornare il genere.
- Completa le definizioni dei metodi:
- Nella classe
Bookesistente, aggiungi un metodo chiamatogetInfoche restituisce una stringa nel formato:"Title by Author is a Genre book."; - Aggiungi un altro metodo chiamato
updateGenreche accetta un parametro,newGenre, e aggiorna la proprietàgenredel libro.
- Nella classe
- Testa i metodi:
- Un'istanza di
Bookchiamatabook1è già stata creata con i valori"The Great Gatsby"," F. Scott Fitzgerald"e"Classic"; - Chiama
getInfoper visualizzare le informazioni sul libro; - Usa
updateGenreper cambiare il genere in"Historical Fiction"; - Chiama nuovamente
getInfoper confermare l'aggiornamento del genere.
- Un'istanza di
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.
- Definire un metodo chiamato
getInfonella classeBook; - Nel metodo
getInfo, restituire una stringa che utilizzathis.title,this.authorethis.genre; - Definire un metodo chiamato
updateGenreche accetta un parametro,newGenre; - Nel metodo
updateGenre, impostarethis.genresunewGenre; - Chiamare
getInfosubook1per visualizzare le informazioni iniziali sul libro; - Utilizzare
updateGenresubook1per cambiare il genere in"Historical Fiction"; - Chiamare nuovamente
getInfosubook1per confermare il genere aggiornato.
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.
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 5
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 2.22
Sfida: Aggiungi Metodi a una Classe
Scorri per mostrare il menu
Compito
Stai lavorando con una classe Book che rappresenta i libri in una biblioteca. Ogni libro ha un titolo, un autore e un genere. Il tuo compito è aggiungere metodi a questa classe per recuperare informazioni e aggiornare il genere.
- Completa le definizioni dei metodi:
- Nella classe
Bookesistente, aggiungi un metodo chiamatogetInfoche restituisce una stringa nel formato:"Title by Author is a Genre book."; - Aggiungi un altro metodo chiamato
updateGenreche accetta un parametro,newGenre, e aggiorna la proprietàgenredel libro.
- Nella classe
- Testa i metodi:
- Un'istanza di
Bookchiamatabook1è già stata creata con i valori"The Great Gatsby"," F. Scott Fitzgerald"e"Classic"; - Chiama
getInfoper visualizzare le informazioni sul libro; - Usa
updateGenreper cambiare il genere in"Historical Fiction"; - Chiama nuovamente
getInfoper confermare l'aggiornamento del genere.
- Un'istanza di
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.
- Definire un metodo chiamato
getInfonella classeBook; - Nel metodo
getInfo, restituire una stringa che utilizzathis.title,this.authorethis.genre; - Definire un metodo chiamato
updateGenreche accetta un parametro,newGenre; - Nel metodo
updateGenre, impostarethis.genresunewGenre; - Chiamare
getInfosubook1per visualizzare le informazioni iniziali sul libro; - Utilizzare
updateGenresubook1per cambiare il genere in"Historical Fiction"; - Chiamare nuovamente
getInfosubook1per confermare il genere aggiornato.
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.
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 5