Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,261 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 ??? }