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

I am confuse in this challengue

The challengue say the next

There are three list items in the index.html file. We want to be able to enter a 0, 1 or 2 in the text field to embolden the list item with the corresponding index. Can you figure out what condition to put into the if statement to make this code work?

who is the correct form for write the correct conditional ?

app.js
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];

       // replace 'false' with a correct test condition on the line below
       if (false) {

           law.style.fontWeight = 'bold';
       } else {
           law.style.fontWeight = 'normal';
       }
    }
});
index.html
<!DOCTYPE html>
<html>
<head>
  <title>Newton's Laws</title>
</head>
<body>
  <h1>Newton's Laws of Motion</h1>
  <ul>
    <li>An object in motion tends to stay in motion, unless acted on by an outside force.</li>
    <li>Acceleration is dependent on the forces acting upon an object and the mass of the object.</li>
    <li>For every action, there is an equal and opposite reaction.</li>
  </ul>
  <input type="text" id="boldIndex">
  <button id="embolden">Embolden</button>
  <script src="app.js"></script>
</body>
</html>

I had the same problem. I tried all sorts of different solutions so if anyone has an idea what the right condition is, please help :)

5 Answers

Steven Parker
Steven Parker
231,248 Points

:point_right: Don't over-think this one, it's quite simple.

All you need to do here is compare the current loop index with the value that the user has placed in the input box. And to make it easy, the challenge has already created a variable named index and assigned it with the numeric value produced by converting the input field.

You just need to test if the loop index (i) is equal to the user's "index".

Tanks for your help, i found the solution.

hi all! I still have not found a solution..help

still doesn't understand the decision answer

Steven Parker
Steven Parker
231,248 Points

So when I said: You just need to test if the loop index (i) is equal to the user's "index".

The code for that would be:

       if (i == index) {

strange...but where did (i) ? it hurts my head

Steven Parker
Steven Parker
231,248 Points

The variable "i" is the loop index. It is right there, being compared to "index" in the statement shown.

I just realized (i) is a loop law ...but I didn't know that you can only put i on ...okay thanks anyway

Justin Hicks
Justin Hicks
14,290 Points

I think I did exactly what you said not to do in the above. I over thought it. After looking at it again you are absolutely correct. const laws = document.getElementsByTagName('li'); sets li to laws. Then later let law = laws[i]; while laws is still the li the [i] tracks the number. Thanks for the clarification Steven!!

Justin Hicks
Justin Hicks
14,290 Points

I put if(index == law) but it doesn't work? Index is the value of the input and law = laws[i] so should that not be the be the same as just i?

Steven Parker
Steven Parker
231,248 Points

No, "law" is a list item (an HTML element), but "i" is just a number (int) counting the loops.

Jasmine Martin
Jasmine Martin
14,307 Points

The question just may need to be reworded :/ Thankful for the forum