In this post, we will be building a Technical Book class that inherits from the Book class from one of the previous challenges. The Technical Book class will have an additional element, edition. We will also provide a function that returns a string that includes the book's edition.
/**
* Goal is to create a Technical Book class that inherits from
* the Book class from one of the previous challenges.
* It also has a fifth element, an edition.
* Provide a function that returns a string that includes the book's edition
*/
First, let's start by creating the Book class and an instance of the TechnicalBook class.
class Book {
constructor(title, author, ISBN, currentCopies) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
this.currentCopies = currentCopies;
}
}
class TechnicalBook extends Book {
constructor(title, author, ISBN, currentCopies, edition) {
super(title, author, ISBN, currentCopies);
this.edition = edition;
}
}
To test the function we will write shortly it is important to create a few books, that we can use.
const firstBook = new TechnicalBook(
"The best technical Book",
"mrs. Author",
123456567,
0,
"first"
);
const secondBook = new TechnicalBook(
"The mostest technical Book",
"mr. Author",
9876543,
4,
"third"
);
const thirdBook = new TechnicalBook(
"The best Book of technicalities",
"mrs. Author Jr.",
1212345121,
5,
"fifth"
);
To get the information about the book, we can create a function that returns a string that includes the book's information.
TechnicalBook.prototype.bookEdition = function () {
return console.log(
`The book ${this.title} was written by ${this.author} with ISBN: ${this.ISBN} and is the ${this.edition} edition. We have ${this.currentCopies} in stock`
);
};
firstBook.bookEdition();
secondBook.bookEdition();
thirdBook.bookEdition();
/*Result:
The book The best technical Book was written by mrs. Author with ISBN: 123456567 and is the first edition. We have 0 in stock
The book The mostest technical Book was written by mr. Author with ISBN: 9876543 and is the third edition. We have 4 in stock
The book The best Book of technicalities was written by mrs. Author Jr. with ISBN: 1212345121 and is the fifth edition. We have 5 in stock
*/
And that's it! With this solution, we were able to create a TechnicalBook class that inherits from the Book class and also has an additional element, edition. We also provided a function that returns a string that includes the book's edition. This can be further customized and expanded upon to suit your needs.
Top comments (1)
Great Job right there. Thanks for sharing.