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 trialCharles Campbell
Full Stack JavaScript Techdegree Student 5,350 PointsI 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
231,124 PointsYou 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
Full Stack JavaScript Techdegree Student 5,350 Pointsyea, 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!
Charles Campbell
Full Stack JavaScript Techdegree Student 5,350 PointsCharles Campbell
Full Stack JavaScript Techdegree Student 5,350 Pointsidk 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
231,124 PointsSteven Parker
231,124 PointsThe 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'
").