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 trialMahfuzur Rahman
3,204 Pointsif-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
231,236 PointsThe 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. Or watch this video on code formatting.
Craig Webber
5,677 PointsCould 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 ??? }