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

Why have a 2 if statments nested inside one another when one can do it?

In the video we are asked to make an eventHandler on the Ul element, then we add the if statement propting if the pressed element is a button, then we add and if statement propting if the pressed element has class remove.

Since we have given all the remove buttons have the class remove, why not just do it like this:

listUl.addEventListener('click',(event)=>{

if(event.target.className == 'remove'){
  let li =event.target.parentNode;
  let ul = li.parentNode;
  ul.removeChild(li);
} 
else if(event.target.className == 'up'){
  let li =event.target.parentNode;
  let prevLi = li.previousElementSibling;
  let ul = li.parentNode;
  ul.insertBefore(li,prevLi);

}
});

2 Answers

in the video, the above code snippit is wrapped like so.

if(event.target.tagName=='button'){ if(event.target.className == 'remove'){ ... ul.insertBefore(li,prevLi); } }

which gives you no benefit. why make an if statement for making sure that your target element is a button, when you already are making sure it has the appropriate classname.

I don't know but I'm not sure your question is clear enough. Can you improve on that?