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

Cant get past this question

please help, I cant figure this one out

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 (law[i] === index)

           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>
Steven Parker
Steven Parker
231,248 Points

This looks like an update to the question already asked.
In future, you can edit or post comments to an existing question to update it.

3 Answers

gata gbemou
seal-mask
.a{fill-rule:evenodd;}techdegree
gata gbemou
Front End Web Development Techdegree Student 9,280 Points

you already set your loop , now read your loop : for (let I=0; I<laws.length; I++){ let law=laws[I]; //variable above is local /since you have three listItems , the loop will run three times starting with the index of 0, 1 and 2 for the desire effect to take place/ //your condition should be applied on I (number of iterations) if(I==index) this will take care of it.

Steven Parker
Steven Parker
231,248 Points

The array is "laws", just "law" is a specific list item so it wouldn't take an index. But you don't want to compare an element to a number anyway.

You only need to compare two numbers here.

im still clueless, why wouldn't this work(index <= 2 && index === laws[i]). how else to you take the index value and assign it to the corresponding li?

Steven Parker
Steven Parker
231,248 Points

The loop goes through the index values of the items, and "index" holds the numeric value that the user has chosen. You're looking for a match between the chosen number and the loop value.

index <= 2 :point_left: checks if user has chosen one of the first 3 items, but since there's only 3 of them this will always be true.

index === laws[i] :point_left: attempts to compare the chosen number with the current list item element, and since these are different types and it's using the type-sensitive comparison operator this will always be false.

i thought the law variable held the current list item and laws[i] held the index value or number. either way I've compared both variables to index. not sure what else there is to do?

Steven Parker
Steven Parker
231,248 Points

"law" and "laws[i]" have the same value .. the first was assigned using the other:

       let law = laws[i];

They both represent the list item element but you only want the numeric value.