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

Challenge working but not passing

My code is as follow

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

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

I don't see anything wrong it the code as I tried on tried on Jsbin https://jsbin.com/gokanudefa/edit?html,js,output but It is not passing here!

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

for( i = 0; i < paragraphs.length; i++ ){
    paragraphs[i].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>

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great, and your code would be fine except that challenges use the "use strict";. Among other things, this means that a global variable may not be implicitly declared. That might not make much sense right now, but your i variable is not being declared with a let or a var. This means that the variable will default to global, which is not allowed by strict mode.

In the setup of your for loop it will need either for(var i = 0 ... or for(let i = 0....

Hope this helps! :sparkles:

Stuart Wright
Stuart Wright
41,118 Points

The issue is that you haven't declared i in the for loop:

for(i=0; i < paragraphs.length; i++ )

You need to add either var or let before your i=0.

As shown by your jsbin example, your code does work correctly for this small example, but can lead to issues once you're working with larger projects - explanation here:

https://stackoverflow.com/questions/6881415/when-is-the-var-need-in-js

Edit: I see that Jennifer beat me to it, but I'll leave my answer here because I think the Stack Overflow answer is worth a read. :)