Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Aggiungi Metodi a una Classe | Padronanza delle Classi e dell'Ereditarietà in JavaScript
Padronanza Avanzata di JavaScript

bookSfida: 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.

  1. Completa le definizioni dei metodi:
    • Nella classe Book esistente, aggiungi un metodo chiamato getInfo che restituisce una stringa nel formato: "Title by Author is a Genre book.";
    • Aggiungi un altro metodo chiamato updateGenre che accetta un parametro, newGenre, e aggiorna la proprietà genre del libro.
  2. Testa i metodi:
    • Un'istanza di Book chiamata book1 è già stata creata con i valori "The Great Gatsby", " F. Scott Fitzgerald" e "Classic";
    • Chiama getInfo per visualizzare le informazioni sul libro;
    • Usa updateGenre per cambiare il genere in "Historical Fiction";
    • Chiama nuovamente getInfo per confermare l'aggiornamento del genere.
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
  • Definire un metodo chiamato getInfo nella classe Book;
  • Nel metodo getInfo, restituire una stringa che utilizza this.title, this.author e this.genre;
  • Definire un metodo chiamato updateGenre che accetta un parametro, newGenre;
  • Nel metodo updateGenre, impostare this.genre su newGenre;
  • Chiamare getInfo su book1 per visualizzare le informazioni iniziali sul libro;
  • Utilizzare updateGenre su book1 per cambiare il genere in "Historical Fiction";
  • Chiamare nuovamente getInfo su book1 per confermare il genere aggiornato.
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 2.22

bookSfida: 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.

  1. Completa le definizioni dei metodi:
    • Nella classe Book esistente, aggiungi un metodo chiamato getInfo che restituisce una stringa nel formato: "Title by Author is a Genre book.";
    • Aggiungi un altro metodo chiamato updateGenre che accetta un parametro, newGenre, e aggiorna la proprietà genre del libro.
  2. Testa i metodi:
    • Un'istanza di Book chiamata book1 è già stata creata con i valori "The Great Gatsby", " F. Scott Fitzgerald" e "Classic";
    • Chiama getInfo per visualizzare le informazioni sul libro;
    • Usa updateGenre per cambiare il genere in "Historical Fiction";
    • Chiama nuovamente getInfo per confermare l'aggiornamento del genere.
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
  • Definire un metodo chiamato getInfo nella classe Book;
  • Nel metodo getInfo, restituire una stringa che utilizza this.title, this.author e this.genre;
  • Definire un metodo chiamato updateGenre che accetta un parametro, newGenre;
  • Nel metodo updateGenre, impostare this.genre su newGenre;
  • Chiamare getInfo su book1 per visualizzare le informazioni iniziali sul libro;
  • Utilizzare updateGenre su book1 per cambiare il genere in "Historical Fiction";
  • Chiamare nuovamente getInfo su book1 per confermare il genere aggiornato.
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 5
some-alt