vendredi 12 février 2021

how to shorten if/else statement

I am trying to shorten a series of if/else statements that I have created. The code works just fine, but it seems redundant at the moment. I was trying to implement a loop to do this, I couldn't figure out how to handle the "indexOf("")" for each statement in the loop.

Here is my code:

    window.addEventListener("load", (event)=>{

      $("div.faq-container").hide();
    if (window.location.href.indexOf("shipping") > -1) {
      $("div.faq-container").eq( "0" ).show();
    }
    else if (window.location.href.indexOf("returns") > -1) {
      $("div.faq-container").eq( "1" ).show();
    }
    else if (window.location.href.indexOf("custom") > -1) {
      $("div.faq-container").eq( "2" ).show();
    }
    else if (window.location.href.indexOf("replacements") > -1) {
      $("div.faq-container").eq( "3" ).show();
    }
    else if (window.location.href.indexOf("mostFAQs") > -1) {
      $("div.faq-container").eq( "4" ).show();
    }
    else if (window.location.href.indexOf("RAD") > -1) {
      $("div.faq-container").eq( "5" ).show();
    }
    else if (window.location.href.indexOf("environmental") > -1) {
      $("div.faq-container").eq( "6" ).show();
    }
    else if (window.location.href.indexOf("USA") > -1) {
      $("div.faq-container").eq( "7" ).show();
    }
    else{
      $("div.faq-container").eq( "0" ).show();
    }

    })

any suggestions?

Aucun commentaire:

Enregistrer un commentaire