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

How can I make this code DRY?

          $('.nav-item a, .nav-brand a, .footer a, .button').click(function() {
    event.preventDefault(); // default action of the event will not be triggered, eg will not change links name
    var windowSize = $(window).width();

    if (windowSize >= 769) {
    $('html, body').animate({
      scrollTop: $($(this).attr('href')).offset().top - 51
    }, 1500);

    }
    else if (windowSize <= 768) {
    $('html, body').animate({
      scrollTop: $($(this).attr('href')).offset().top - 102
    }, 1500);

    }

    return false;
  });
          ```

1 Answer

You could put the animate() call in a separate function that accepts an offset parameter, then call it in each conditional passing 51 in the first and 102 in the second.

Struggling a little here, can you give me a hint?

Put this scrollTop: $($(this).attr('href')).offset() in a variable , and then put the variable in palce of these line of code. Maybe thatll work?