Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Practice Object Interaction Checking Out and Returning a Book Solution: A Better Way With Setter Methods

AJ Zhang
AJ Zhang
8,946 Points

I'm having problem with setting Due Date: Can't figure out what I did wrong.

'''

class Patron {

constructor (name,email) { this.name = name; this.email = email; this.currentBook = null; }

checkOut(book) {
     this.currentBook = book; 
     book.out = true;
     book.patron=this;
}

returnBook(book) {
     this.currentBook = null;
     book.out = false;
     book.patron = null;
}

set out(out){
     this._out=out;

     if (_out) {
     const newDueDate = new Date();
     newDueDate.setDate (newDueDate.getDate()+14);
     this.dueDate = newDueDate;
    } else {
      this.dueDate = null;
    }
 }


get out(){
  return this._out;
}

} '''

4 Answers

Steven Parker
Steven Parker
231,248 Points

When you test the backing variable, be sure to include the "this." prefix:

    if (this._out) {  // or just "if (out)" without underscore
AJ Zhang
AJ Zhang
8,946 Points

I moved it to Book.js file and it worked.

AJ Zhang
AJ Zhang
8,946 Points

Thanks for getting back to me so soon. I tried but still not working. Below is what the console returns. Why is _out remains false?

Patron {name: "AJ", email: "123@gmail.com", currentBook: Book} currentBook: Book author: "J.K. Rowling" dueDate: null isbn: "978-0439708180" out: true patron: Patron {name: "AJ", email: "123@gmail.com", currentBook: Book} title: "Harry Potter and the Sorcerer's Stone" out: false __proto_: Object email: "123@gmail.com" name: "AJ" out: undefined

Steven Parker
Steven Parker
231,248 Points

I'm confused, is "out" a property of a patron, or a book?

Instructions for code formatting can be found in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:
Or watch this video on code formatting.

AJ Zhang
AJ Zhang
8,946 Points

This is embarrassing. I put everything in Patron.js. I'm struggling a bit with this topic ... Thanks for your help!

Heidi Vasterling-Ford
Heidi Vasterling-Ford
7,806 Points

AJ, I am having the same issue with the due date. I too have the setter and getter in the Patron.js file, where is it supposed to go?