Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
215,987 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 ??? }