title: Want to reduce nested if else statement to reduce the cognitive complexity?
body: <p>//I have a function that returns the search result based on the input user put in the search //field. The code is build using a nested if-else statement which in result adding the cognitive //complexity of the code. I want to reduce the code complexity by reducing the if-else //statements.
  //here is my code</p>
  <pre><code>(function($) {
    $(document).ready(function() {
      myFunction();
    });
    function myFunction() {
      $('#searchInput').keyup(function() {
        var  i;
        var input = document.getElementById(&quot;searchInput&quot;);
        var filter = input.value.toUpperCase();
        var table = document.getElementById(&quot;tabledisplay&quot;);
        var tr = table.getElementsByTagName(&quot;tr&quot;);
        var e = document.getElementById(&quot;searchtype&quot;);
        var strUser = e.options[e.selectedIndex].value;
        for (i = 0; i &lt; tr.length; i++) {
          if (strUser == 'Date') {
            td = tr[i].getElementsByTagName(&quot;td&quot;)[1];
            if (td) {
            var txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
                tr[i].style.display = &quot;&quot;;
              }
              else {
              tr[i].style.display = &quot;none&quot;;
              }
            }
          }
          else if (strUser == 'City') {
            td = tr[i].getElementsByTagName(&quot;td&quot;)[2];
            if (td) {
              txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
                tr[i].style.display = &quot;&quot;;
              }
              else {
              tr[i].style.display = &quot;none&quot;;
              }
            }
          }
          else if (strUser == 'State') {
            td = tr[i].getElementsByTagName(&quot;td&quot;)[3];
            if (td) {
              txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
                tr[i].style.display = &quot;&quot;;
              }
              else {
              tr[i].style.display = &quot;none&quot;;
              }
            }
          }
          else if (strUser == 'Inspection Number') {
            td = tr[i].getElementsByTagName(&quot;td&quot;)[5];
            if (td) {
              txtValue = td.textContent || td.innerText;
              if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
                tr[i].style.display = &quot;&quot;;
              }
              else {
              tr[i].style.display = &quot;none&quot;;
              }
            }
          }
        }
      });
    }
  })(jQuery, Drupal, drupalSettings);
  </code></pre>