How to return proper value from a class method

I know the question is probably simple for most of you, but I’ve been stuck for a whole day to solve this dilemma. I think that the problem is in setCurrent(book) or in finishCurrentBook method.

Their tasks are:

setCurrent(book) – makes the book currently being read in the library, initializes the currentBook field of the current book to true and returns “You just started reading: Book_name”. If this book is already current, return “You are already reading: Book_name”. When trying to add another book as the current book before the current book has been read, return “You can’t add this book as the current book before finishing: currentBook_name”. One book from the library can be current at the moment.

finishCurrentBook() marks the current read book as read. Sets the last read book to the book you just finished reading, initializes the currentBook field of the current book to false In the case when the currentBook is not defined, displays the message “You’re not reading any book right now…”

my code is simple and the test, which I cannot modify should return
true
false
but instead, I’ve got undefined, undefined.
Feel free to modify classes, but not the test

class Book {
  constructor(title, genre, author) {
    this.title = title;
    this.genre = genre;
    this.author = author;
    this.finishedRead = false;
  }
}
class Library {
  constructor(name) {
    this.name = name;
    this.allBooks = [];
    this.currentBook = false;
    this.lastBook = null;
  }

  getAllBooks() {
    return this.allBooks;
  }

  getCurrentBook() {
    if (this.currentBook) {
      return `You are currently reading: ${this.currentBook.title}`;
    } else {
      return "You're not reading any book at the moment";
    }
  }

  getLastBook() {
    if (this.lastBook) {
      return `The last book you read was: ${this.lastBook.title}`;
    } else {
      return "You haven't read anything in a while!";
    }
  }

  getBooksToRead() {
    return this.allBooks.filter((book) => !book.finishedRead).length;
  }

  getFinishedBooks() {
    const finishedBooks = this.allBooks.filter((book) => book.finishedRead);
    if (finishedBooks.length > 0) {
      return finishedBooks;
    } else {
      return 'You have not yet read any books from your library!';
    }
  }

  addBook(book) {
    this.allBooks.push(book);
  }

  setCurrent(book) {
    if (this.currentBook === book) {
      return `You are already reading: ${book.title}`;
    } else if (this.currentBook) {
      return `You can't add this book as the current book before finishing: ${this.currentBook.title}`;
    } else {
      this.currentBook = book;
      book.finishedRead = false; // Ensure the book is marked as unread
      return `You just started reading: ${book.title}`;
    }
  }

  finishCurrentBook() {
    if (this.currentBook) {
      this.currentBook.finishedRead = true;
      this.lastBook = this.currentBook;
      this.currentBook = null;
      return `You have finished reading: ${this.lastBook.title}`;
    } else {
      return "You're not reading any book right now...";
    }
  }
}


// The test, which I can't modify


const myLibrary = new Library("My books");
const javaScript = new Book("JS", "IT", "Flanagan");
const winds = new Book("Winds", "Historical", "Hannah");
const ring = new Book("The Ring", "Fantasy ", "Tolkien");

const booksList = [javaScript, winds, ring];
booksList.forEach((b) => myLibrary.addBook(b));

myLibrary.setCurrent(ring);
console.log(ring.currentBook);
myLibrary.finishCurrentBook();
console.log(ring.currentBook);

  • 2

    In your test script, you’re trying to access currentBook on the Book instances (javaScript, winds, ring) rather than on the Library instance (myLibrary). The currentBook property is defined in the Library class, not in the Book class.

    – 

  • So, what should I do? Make Book class extends from Library?

    – 

  • You have some basic flaws in your code. The currentBook member of library is currently being initialized as a bool value, but then used like a book value in the setCurrent method. Rename it to something like haveCurrentBook and change the if to (if this.haveCurrentBook). Then, change the code to assign the current book to this.haveCurrentBook = true; book.currentBook = book (note: this is weird but matches the usage in the test). You will need to set book.currentBook = null in the finishCurrentBook method as well. Bottom line, you are never setting book.currentBook to a value anywhere.

    – 




Leave a Comment