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 JavaScript and the DOM (Retiring) Traversing the DOM Child Traversal

gugulethu
gugulethu
6,292 Points

Why do i keep getting Task 1 is no longer passing?

I'm doing the last code review of the JS and the DOM section. Task 1 is correct, but I keep being stopped at Task 2 for an error in task 1.

app.js
const section = document.querySelector('section');
let paragraphs = section.children;

paragraphs.style.color = "blue";
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Child Traversal</title>
    </head>
    <body>
        <section>
            <p>This is the first paragraph</p>
            <p>This is a slightly longer, second paragraph</p>
            <p>Shorter, last paragraph</p>
        </section>
        <footer>
            <p>&copy; 2016</p> 
        </footer>
        <script src="app.js"></script>
    </body>
</html>
gugulethu
gugulethu
6,292 Points

Ok so the correct code should be:

const section = document.querySelector('section'); let paragraphs = section.children;

for ( g = 0; g < paragraphs.length; g++ ) { paragraphs[g].style.color = "blue"; }

To change all the p's.

This code works on codepen. Still the same problem on Treehouse.

2 Answers

Steven Parker
Steven Parker
231,271 Points

:point_right: You should have gotten a hint with your error.

When you tried your loop in the challenge, the error should have included this hint: "Strict mode forbids implicit creation of global property 'g'".

So all you need to do now is add a "var" or "let" to your loop initialization clause:

for (let g = 0; g < paragraphs.length; g++ )

It worked as-is on codepen because strict mode is not enabled by default there.

gugulethu
gugulethu
6,292 Points

Of course, thanks so much