如何从类方法返回正确值?JS类方法逻辑错误排查求助
我知道这个问题对大多数人来说可能很简单,但我已经卡了一整天。我觉得问题出在setCurrent(book)或者finishCurrentBook方法里。
功能要求
setCurrent(book) – 将指定书籍设为图书馆当前阅读书籍,将该书籍的
currentBook字段设为true,返回"You just started reading: Book_name"。如果该书籍已是当前阅读书籍,返回"You are already reading: Book_name"。若当前书籍未读完就尝试设置另一本为当前书籍,返回"You can't add this book as the current book before finishing: currentBook_name"。同一时刻图书馆只能有一本当前阅读书籍。
finishCurrentBook() – 将当前阅读书籍标记为已读,将最近读完的书籍设为刚完成的这本,将当前书籍的
currentBook字段设为false。若未定义currentBook,返回消息"You're not reading any book right now..."。
问题现状
测试代码不可修改,预期输出true、false,但实际得到undefined、undefined。允许修改类实现,但不能改动测试代码。
现有代码
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..."; } } } // 不可修改的测试代码 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);
解决方案
问题核心是没有按照功能要求操作Book实例的currentBook字段,现有代码只处理了finishedRead,完全忽略了currentBook的设置。
修改步骤:
- 初始化Book类的
currentBook字段,避免初始值为undefined:
class Book { constructor(title, genre, author) { this.title = title; this.genre = genre; this.author = author; this.finishedRead = false; this.currentBook = false; // 新增:初始化currentBook为false } }
- 修改
setCurrent方法,在设置当前书籍时,将书籍的currentBook设为true:
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; book.currentBook = true; // 新增:标记该书籍为当前阅读状态 return `You just started reading: ${book.title}`; } }
- 修改
finishCurrentBook方法,在完成阅读时,将书籍的currentBook设为false:
finishCurrentBook() { if (this.currentBook) { this.currentBook.finishedRead = true; this.currentBook.currentBook = false; // 新增:取消该书籍的当前阅读标记 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..."; } }
修改后运行测试代码,console.log会依次输出true和false,符合预期。
内容的提问来源于stack exchange,提问作者antonkornilov-ua

