Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Practice Object Interaction Checking Out and Returning a Book Solution: Charging Fines to Patrons

My chargeFines method

My chargeFines method is this:

chargeFines = () => {
        this.patrons.filter(patron => patron.out)
            .forEach(patron => {
                const today = new Date();
                if (today > patron.currentBook.dueDate) {
                    const day = today.getDate();
                    const due = patron.currentBook.dueDate.getDate();
                    const daysLate = day - due;
                    patron.balance = daysLate * this.dailyFine;

I think it is actually a more elegant solution than Ashley's, but could someone with more experience critique it and tell me if I am on the right track? Thanks!

1 Answer

Steven Parker
Steven Parker
215,370 Points

Did you test this? I'm wondering because arrow functions do not define a "this" like conventional functions do.

After I posted this I did end up testing it and it does produce the desired behaviour. However I am now wondering if there is a more efficient way of writing the function. Steven do you think you could point me to a resource you know of explaining this in the context of arrow functions?

Steven Parker
Steven Parker
215,370 Points

MDN is my "go to" reference for all things front-end. Here's the page on Arrow Functions.
Look for the highlighted section titled "No separate this".

But also note that MDN advises "Arrow function expressions are ill suited as methods..."

Thank you very much.