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 trialJake Kobs
9,215 PointsIs this code ok? I know Guil traversed from different elements at times, but is mine fine?
// STARTING POINT
const list = document.querySelector('.list');
// 1: Store the first child of the `ul` in the variable `firstItem`
const firstItem = list.firstElementChild;
firstItem.style.backgroundColor = '#04c5e6';
// 2: Using traversal, store the second list item in a variable named `nextItem`
const nextItem = firstItem.nextElementSibling;
nextItem.style.backgroundColor = '#b7c7d0';
// 3: Store the last child of the `ul` in a variable named `lastItem`
const lastItem = list.lastElementChild;
lastItem.style.backgroundColor = '#57d6ab';
// 4: Using traversal, store the second-to-last list item in a variable named `prevItem`
const prevItem = list.lastElementChild.previousElementSibling;
prevItem.style.backgroundColor = '#f36f49';
// 5: Store the nested div in a variable named `banner`
const banner = list.previousElementSibling;
banner.className = 'banner';
// 6: Using traversal, store the wrapper div in a variable named `wrapper`
const wrapper = list.parentNode;
wrapper.style.backgroundColor = '#fcfcfc';
// 7: Using traversal, store the body in a variable named `body`
const body = list.parentNode.parentNode;
body.style.backgroundColor = '#f8fdf3';
2 Answers
Steven Parker
231,248 PointsThis is certainly functional, but it's more efficient to use the other variables and it makes the code more concise.
Functionality is certainly the most important criterion, but it's better to achieve all three if you can.
Obe Juarez
6,357 PointsFor #4, you don't need to add 'list'. You could just write it this way: const prevItem = lastItem.previousElementSibling; no need to add list since your already defined what the latItem is
Gary A
3,753 PointsGary A
3,753 PointsTrue. but you're adding a dependency if you do that. I think both approaches are acceptable, depending on what you are doing.