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: Charging Fines to Patrons

Samuel Kleos
seal-mask
.a{fill-rule:evenodd;}techdegree
Samuel Kleos
Front End Web Development Techdegree Student 13,728 Points

Confusion. . . 😵‍💫 Why is my daysOverDue property negative?

In my Library class I used filter and reduce to generate a .daysOverDue value from a book using a getter method in the book class. Also, a setter method out() is used to set .dueDate to -14 days (in the past):

chargeFines() {
    const now = new Date();

    const latePatrons = this.patrons.filter(patron => 
        (patron.currentBook !== null && patron.currentBook.dueDate < now)
    );

    for (let patron of latePatrons) {
        const daysLate = Math.floor( (now.getTime() - patron.currentBook.dueDate.getTime()) / (1000 * 60 * 60 * 24));
        patron.balance += this.dailyFine * daysLate;
    }
}

My solution:

collectFines() {
        let today = new Date();
        console.log(today.getDate())
        return this.patrons
            .filter(patron => (patron.currentBook.daysOverDue > 0))
            .reduce((collector, patron) => { 
                console.log(patron.currentBook.dueDate.getDate());
                patron.balance = -patron.currentBook.daysOverDue * this.dailyFine;
                return collector + -patron.balance;
        }, 0);
    }

Here is my Book class where dueDate is set and daysOverDue is calculated:

set out(value) {
        this._out = value;
        if (value) {
            let today = new Date();
            today.setDate(today.getDate() - 14);
            this.dueDate = today;
        } else {
            this.dueDate = null;
        }
    }
get daysOverDue() {
        let today = new Date();
        let daysOverDue = today.getDate() - this.dueDate.getDate();
        return daysOverDue
    }

ASIDE: What's the difference between

this.dueDate = today.setDate(today.getDate() - 14);

and

today.setDate(today.getDate() - 14);
this.dueDate = today;

1 Answer

Travis Alstrand
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Travis Alstrand
Treehouse Teacher

I found this page which shows an example of using getDate and setDate about halfway down and subtracting days from them. I'm not exactly sure why you're getting a negative number back, if the date it has is less than 14 would it go negative and not loop through the end of the previous month?

I hope this is helpful, if not please let me know!

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree
Travis Alstrand
Treehouse Teacher

If you have a GitHub repo that you can share the link to, it'd be easier to play with it and see if we can come up with a solution. But I'm unfortunately not 100% sure why it's doing that to you, my apologies.