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 DOM Scripting By Example Editing and Filtering Names Fix DOM Manipulation Code

Juan Álvarez
Juan Álvarez
10,859 Points

Doubt

I am stuck in this mini challenge. Index is the number that you collect in the field that must be === to any of this three numbers (0,1,2) to be true and show the style bold. Otherwise it will be normal. So, how I can collect the three index of the array law ? I tried with the indexOf method with no luck... thank you.

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

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

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

       console.log(law.indexOf(laws[i]));
       // replace 'false' with a correct test condition on the line below
       if ( index === law) ) {
           law.style.fontWeight = 'bold';
       } else {
           law.style.fontWeight = 'normal';
       }
    }
});

1 Answer

Frederic Ronaldi
Frederic Ronaldi
13,732 Points

Hi, it seems like you are comparing the index with each li document (law). you only need to compare the index in the input with i in the loop because it starts counting from 0 to 2. (laws.length)

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

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

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

       console.log(law.indexOf(laws[i]));
       // replace 'false' with a correct test condition on the line below
       if ( index === i) ) {
           law.style.fontWeight = 'bold';
       } else {
           law.style.fontWeight = 'normal';
       }
    }
});
Juan Álvarez
Juan Álvarez
10,859 Points

wow, sometimes we look for difficult solutions being, in fact, much more easy. Thank you.