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

Bolivar Arguello
seal-mask
.a{fill-rule:evenodd;}techdegree
Bolivar Arguello
Front End Web Development Techdegree Student 9,429 Points

Editing and Filtering names

I've been stuck on this single line of code for a few days now. I just do not know what to replace with the first if condition statement as required. I already tried else if statement but does not seem to work and so many other solutions but unable to solve it. I don't even know what video or link to refer to. thanks in advance for any help provided.

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 of 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>

2 Answers

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 38,304 Points

Hi Bolivar. This is a little complicated so hopefully I can explain it instead of just giving you the answer.

So first of all what is index? Index is indexText.value which is what the user entered.

Next we need the li elements. laws uses getElementsByTagName('li') to get an array of the li elements. So laws[0] will be the first and laws[1] will be the next and laws[2] will be the next element.

If you look inside the for statement they have let law = laws[i]; So each time through the for loop law will be set to the next li element using the index i.

So in your if you simply need to compare index with i. When they are equal that is the one li element you want to bold.

Hope this helps and be sure to post back to the community if you have more questions