← Back to challenges

Book Shelf

JavaScriptHardobjectslanguage_fundamentalsclasses

Instructions

Create a Book constructor that has two properties :

  1. Title
  2. Author

and two methods:

  1. A method named getTitle that returns: "Title: " + the instance title.
  2. A method named getAuthor that returns: "Author: " + the instance author.

and instantiate this constructor by creating 3 new books:

  1. Pride and Prejudice - Jane Austen (PP)
  2. Hamlet - William Shakespeare (H)
  3. War and Peace - Leo Tolstoy (WP)

The name of the new object instances PP, H, and WP, respectively.

For instance, if I instantiated the following book using this Book constructor function:

  • Harry Potter - J.K. Rowling (HP)

I would get the following properties and methods:

Examples

HP.title ➞ "Harry Potter"
HP.author ➞ "J.K. Rowling"
HP.getTitle() ➞ "Title: Harry Potter"
HP.getAuthor() ➞ "Author: J.K. Rowling"

Notes

  • Remember, after you've finished writing the constructor function, you must instantiate it through the creation of new objects.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.