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 trialJonas Wu
13,928 PointsThe code can run but i got the Bummer"Did you select all 7 list items?"
Hello all,
You can see the list items get the 7 colors. But it says I didn't select all the item. Could anyone give me some hints?
thank you :)
const listItems = document.getElementById('rainbow')
var colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];
for(let i = 0; i < colors.length; i ++) {
listItems.children[i].style.color = colors[i];
}
<!DOCTYPE html>
<html>
<head>
<title>Rainbow!</title>
</head>
<body>
<ul id="rainbow">
<li>This should be red</li>
<li>This should be orange</li>
<li>This should be yellow</li>
<li>This should be green</li>
<li>This should be blue</li>
<li>This should be indigo</li>
<li>This should be violet</li>
</ul>
<script src="js/app.js"></script>
</body>
</html>
2 Answers
Jonas Wu
13,928 PointsHello guys,
I made some change, and it passed in the end:
const listItems = document.querySelectorAll('li:nth-child(n)');
var colors = ["#C2272D", "#F8931F", "#FFFF01", "#009245", "#0193D9", "#0C04ED", "#612F90"];
for(let i = 0; i < colors.length; i ++) {
listItems[i].style.color = colors[i];
}
Could anyone tell me what's the difference between these 2 way of coding? Thanks !
Justin Horner
Treehouse Guest TeacherHello Jonas,
Great job on finding a solution to the challenge! I believe the reason the challenge isn't passing is due tolistItems
being set to the ul
instead of a collection. You should only have to modify the first line to get the challenge to pass. Try setting it to something like this:
var listItems = document.querySelectorAll('ul li');
...
I hope this helps.