mercredi 9 septembre 2015

How to shorten my nested if/else statement?

I have the following js code:

 var currentUrl = location
        if(/s01/.test(currentUrl)||/s02/.test(currentUrl)){
            $('#info,#name').removeClass('onselect').addClass('offselect');
            if(/s01/.test(currentUrl)){
                $('#AA').removeClass('offselect').addClass('onselect');
            } else if(/s02/.test(currentUrl)){
                $('#BB').removeClass('offselect').addClass('onselect');
            }
        } else {
           .....
        } 

The code have the same part in nested if/else statement:

if(/s01/.test(currentUrl){

} else if(/s02/.test(currentUrl){

}

Can I shorten it to something like:

if(a){

} else if(b){

}

Or any suggestions? Thx :-)

Aucun commentaire:

Enregistrer un commentaire