Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript DOM Scripting By Example Editing and Filtering Names Fix DOM Manipulation Code

Charles Campbell
seal-mask
.a{fill-rule:evenodd;}techdegree
Charles Campbell
Full Stack JavaScript Techdegree Student 5,350 Points

I figured this challenge out but probably in the wrong way. Need some help.

I noticed that const laws was returning an HTML collection and not a normal array. I didn't know how to get an array index from something like this. So I ended up targeting the ul children and using... ul.item(index) == ul.item(i).

we never went over .item() - I googled for it. so I'm trying to figure out what concept I missed/forgot that would allow me to do this the way treehouse expected it?

Overall I realize the challenge is trying to get a list item to match the input we type in. Here is the code so you can see how I did it

const laws = document.getElementsByTagName('li'); const indexText = document.getElementById('boldIndex'); const button = document.getElementById('embolden'); const ul = document.querySelector('ul'); const children = ul.children;

button.addEventListener('click', (e) => { const index = parseInt(indexText.value, 10);

for (let i = 0; i < laws.length; i += 1) {


   // replace 'false' with a correct test condition on the line below
   if (children.item(index) == children.item(i)) {

       law.style.fontWeight = 'bold';
   } else {
       law.style.fontWeight = 'normal';
   }

console.log(children[1]); } });

2 Answers

Steven Parker
Steven Parker
215,357 Points

You got a bit fancy here. You don't actually need to use the index values to access the item(s) they point to, you can simply compare the index values to each other directly:

   if (index == i) {
Charles Campbell
seal-mask
.a{fill-rule:evenodd;}techdegree
Charles Campbell
Full Stack JavaScript Techdegree Student 5,350 Points

idk if you can see the original code or not but what was the point of having
let law = laws[i]; in the code at all? i tried index == law and index == laws[i]. neither of those work. your answer is simple and to the point and my brain went right over it trying to compare other things.

Steven Parker
Steven Parker
215,357 Points

The reason for "law = laws[i]" is so that "law" will be available as a reference to change the style (for example: "law.style.fontWeight = 'bold'").

Charles Campbell
seal-mask
.a{fill-rule:evenodd;}techdegree
Charles Campbell
Full Stack JavaScript Techdegree Student 5,350 Points

yea, i realized that right after I asked it. Since I'm new to coding my brain saw that line as how I should go about pointing to the array items. Thanks for the help!