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

Martin Chaffee
seal-mask
.a{fill-rule:evenodd;}techdegree
Martin Chaffee
Front End Web Development Techdegree Student 15,278 Points

Challenge task 2 failing: "It looks like Task 1 is no longer passing".

I don't understand what I'm doing wrong. In fact, I've written the exact same HTML and JS files in Sublime and opened them in Chrome, and they work. It's driving me crazy. Can anyone please tell me what I'm doing wrong? It's making me feel like I'm missing some fundamental concept in JS...

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

for (i=0; i < paragraphs.length; i+= 1) {
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>
Robertin Datkov
Robertin Datkov
11,558 Points

Greetings Martin Chaffee, I had a look at the code and I've noticed that you misspelled the method. It's actually getElementsByTagName. Which will return an array of all 'section' elements you currently have in the DOM. So in your case, you would like to select the first one:

const section = document.getElementsByTagName('section');
let paragraphs = section[0].children;

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

Hope this helps!

2 Answers

Wanderson Macêdo
Wanderson Macêdo
9,266 Points

Hi Martin Chaffee, the Robertin Matkov's answer is right. Have you tested his code?

Martin Chaffee
seal-mask
.a{fill-rule:evenodd;}techdegree
Martin Chaffee
Front End Web Development Techdegree Student 15,278 Points

Yes, I sorted it out. I had "i=0" rather than "let i=0". It seems as though Sublime automatically establishes "i" as a variable, which was why the code was working on my text editor but was failing in the challenge.