How to remove a repeated function outside where it is needed and call it

How to remove a repeated function outside where it is needed and call it

Hi,

I am trying to build an accordion type structure that can be used by CMS users without need for them to do any coding. To link to and open the accordion section required by the user - they just count down to which accordion section that they want to link to and add #section1 etc to the end of the link location. I have an if else statement which does the rest for them, however, it repeats this code which makes the script really long:

$(class).nextAll().each( function() {
           if ($(this).filter('h3').length) {
              return false;
           }

           $(this).show(scroll);
});
$(this).removeClass("maximized").addClass("minimized");

I was hoping to remove this code to be a separate function so it does not have to be repeated and then call it into the if else statement each time it is needed - however I am completely stumped on how I would go around doing this. Any help I could get would be really appreciated - I am booked on a course to learn jquery, however, it is not for a another month and really need to put this code to use.

Here is a larger code segment

/* add a class to the h3 to enable Url selection and increment each time by 1*/
$(".accordionstyle h3").each(function(i){
      $(this).addClass("section"+i)
});

/*reading the url anchor and opening the related accordion*/
var theUrl = window.location.href
var hashValue = theUrl.split('#')[1];
if (hashValue == 'section1'){
var class = $(".section0");
$(class).nextAll().each( function() {
           if ($(this).filter('h3').length) {
              return false;
           }

           $(this).show(scroll);
});
$(this).removeClass("maximized").addClass("minimized");
}else if (hashValue == 'section2'){
var class = $(".section1");
$(class).nextAll().each( function() {
           if ($(this).filter('h3').length) {
              return false;
           }

           $(this).show(scroll);
});
$(this).removeClass("maximized").addClass("minimized");
}else if (hashValue == 'section3'){
var class = $(".section2");
$(class).nextAll().each( function() {
           if ($(this).filter('h3').length) {
              return false;
           }

           $(this).show(scroll);
});
}else if (hashValue == 'section4'){
var class = $(".section3");
$(class).nextAll().each( function() {
           if ($(this).filter('h3').length) {
              return false;
           }

           $(this).show(scroll);
});
}else if (hashValue == 'section5'){