Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Haaste: Lisää Metodit Luokkaan | JavaScript-luokkien ja Perinnän Hallinta
Edistynyt JavaScript-Osaaminen

bookHaaste: Lisää Metodit Luokkaan

Tehtävä

Työskentelet Book-luokan kanssa, joka edustaa kirjoja kirjastossa. Jokaisella kirjalla on nimi, kirjoittaja ja genre. Tehtävänäsi on lisätä tähän luokkaan metodit tietojen hakemista ja genren päivittämistä varten.

  1. Täydennä metodimäärittelyt:
    • Lisää olemassa olevaan Book-luokkaan metodi nimeltä getInfo, joka palauttaa merkkijonon muodossa: "Title by Author is a Genre book.";
    • Lisää toinen metodi nimeltä updateGenre, joka ottaa yhden parametrin, newGenre, ja päivittää kirjan genre-ominaisuuden.
  2. Testaa metodeja:
    • Book-luokan olio nimeltä book1 on jo luotu arvoilla "The Great Gatsby", "F. Scott Fitzgerald" ja "Classic";
    • Kutsu getInfo-metodia tulostaaksesi tietoa kirjasta;
    • Käytä updateGenre-metodia vaihtaaksesi genren arvoon "Historical Fiction";
    • Kutsu getInfo uudelleen varmistaaksesi genren päivityksen.
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
  • Määrittele getInfo-niminen metodi Book-luokkaan;
  • getInfo-metodissa palauta merkkijono, joka käyttää this.title, this.author ja this.genre;
  • Määrittele updateGenre-niminen metodi, joka ottaa yhden parametrin, newGenre;
  • updateGenre-metodissa aseta this.genre arvoksi newGenre;
  • Kutsu getInfo-metodia book1-oliolle tulostaaksesi kirjan alkuperäiset tiedot;
  • Käytä updateGenre-metodia book1-oliolle vaihtaaksesi genren arvoon "Historical Fiction";
  • Kutsu getInfo-metodia uudelleen book1-oliolle varmistaaksesi päivitetyn genren.
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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 5

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 2.22

bookHaaste: Lisää Metodit Luokkaan

Pyyhkäise näyttääksesi valikon

Tehtävä

Työskentelet Book-luokan kanssa, joka edustaa kirjoja kirjastossa. Jokaisella kirjalla on nimi, kirjoittaja ja genre. Tehtävänäsi on lisätä tähän luokkaan metodit tietojen hakemista ja genren päivittämistä varten.

  1. Täydennä metodimäärittelyt:
    • Lisää olemassa olevaan Book-luokkaan metodi nimeltä getInfo, joka palauttaa merkkijonon muodossa: "Title by Author is a Genre book.";
    • Lisää toinen metodi nimeltä updateGenre, joka ottaa yhden parametrin, newGenre, ja päivittää kirjan genre-ominaisuuden.
  2. Testaa metodeja:
    • Book-luokan olio nimeltä book1 on jo luotu arvoilla "The Great Gatsby", "F. Scott Fitzgerald" ja "Classic";
    • Kutsu getInfo-metodia tulostaaksesi tietoa kirjasta;
    • Käytä updateGenre-metodia vaihtaaksesi genren arvoon "Historical Fiction";
    • Kutsu getInfo uudelleen varmistaaksesi genren päivityksen.
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
  • Määrittele getInfo-niminen metodi Book-luokkaan;
  • getInfo-metodissa palauta merkkijono, joka käyttää this.title, this.author ja this.genre;
  • Määrittele updateGenre-niminen metodi, joka ottaa yhden parametrin, newGenre;
  • updateGenre-metodissa aseta this.genre arvoksi newGenre;
  • Kutsu getInfo-metodia book1-oliolle tulostaaksesi kirjan alkuperäiset tiedot;
  • Käytä updateGenre-metodia book1-oliolle vaihtaaksesi genren arvoon "Historical Fiction";
  • Kutsu getInfo-metodia uudelleen book1-oliolle varmistaaksesi päivitetyn genren.
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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 5
some-alt