Hỏi về Abstract Class trong JavaScript

Chào mọi người, mình gặp một bài trên HackerRank về Abstract Class ở đây. Đoạn code mình viết khi chạy thì in ra màn hình đúng format nhưng HackerRank báo là wrong answer như hình. Mình muốn hỏi mình cần sửa chỗ nào?

Đây là câu hỏi

Given a Book class and a Solution class, write a MyBook class that does the following:

  • Inherits from Book
  • Has a parameterized constructor taking these parameters:
    1. string
    2. string
    3. int
  • Implements the Book class’ abstract display() method so it prints these lines:
    1. , a space, and then the current instance’s .
    2. , a space, and then the current instance’s .
    3. , a space, and then the current instance’s .

Ví dụ input là

The Alchemist
Paulo Coelho
248

In ra theo format như sau:

Title: The Alchemist
Author: Paulo Coelho
Price: 248

Còn đây là đoạn code mình viết bằng javascript.

// Declare your class here.
class MyBook extends Book {
    /**   
    *   Class Constructor
    *   
    *   @param title The book's title.
    *   @param author The book's author.
    *   @param price The book's price.
    **/
    // Write your constructor here
    constructor(title, author, price) {
        super(title, author);
        this.price = price;
    }
    /**   
    *   Method Name: display
    *   
    *   Print the title, author, and price in the specified format.
    **/
    // Write your method here
    display() {
        console.log('Title: ' + this.title);
        console.log('Author: ' + this.author);
        console.log('Price' + this.price);
    }
    // End class
}

sửa lại : console.log('Price: ' + this.price);

1 Like

Nếu sửa this.price -> price thì sẽ báo lỗi là ReferenceError: price is not defined
Bạn định sửa dòng đấy như thế nào?

Mình tìm ra lỗi rồi. Cảm ơn bạn. Sửa thành console.log('Price: ' + parseInt(this.price));. Mình chưa đổi string sang int.

dòng code của bạn format chưa đúng với đề.

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?