JavaScript DOM Scripting By Example Editing and Filtering Names Filter Invitees Who Have Not Responded

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

if-else statement inside if statement

I'm having difficulties in understanding the if-else statement inside the if statement.

if (isChecked) {
    for (let i=0; i<lis.length; i+=1) {
      let li=lis[i]; 
      if (li.className==="responded") {
        li.style.display='';// to keep the initial display style which is to show
      } else {
        li.style.display="none"; //to hide the elements
      }
    } 
  } else {
      for (let i=0; i<lis.length; i+=1) {
      let li=lis[i]; 
      li.style.display=''; // why do we need this again? 
    }
  }

2 Answers

Steven Parker
Steven Parker
171,342 Points

The outer "if...else" is just testing "isChecked". When it is "true", the inner one is part of a loop that goes through every list item and those which have the class "responded" are displayed, and those which do not are hidden.

On the other hand, when "isChecked" is not set, then every list item is shown.

For future questions, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Craig Webber
Craig Webber
5,677 Points

Could you be more specific on what you do not understand.

if (li.className==="responded") { //Do you not understand what is being checked here??? li.style.display=''; //Do you not understand what is being done here ??? } else { li.style.display="none"; //Do you not understand what is being done here ??? }