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

fix-dom-manipulation-code

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. i cant figure it out

app.js
const laws = document.getElementsByTagName('li');
const indexText = document.getElementById('boldIndex');
const button = document.getElementById('embolden');
const input = document.querySelector("input");


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

4 Answers

Steven Parker
Steven Parker
231,275 Points

The challenge code already has conveniently converted the user's input into a number, and put it into the variable "index". So now all you need to do is compare that to the loop variable to see if they match.

Yes i tried it but it didnt work

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 === index) {

           law.style.fontWeight = 'bold';
       } else {
           law.style.fontWeight = 'normal';
       }
    }
});
Steven Parker
Steven Parker
231,275 Points

But "law" isn't the loop variable, and it's also not a number. It's an element reference that will be used to making the style change.

What you want to do is to compare "index" (a number) with the loop variable, which is also a number.

Omg it's (i === index) 🤦 it took me days.

Thanks

I tried everything if (law.value === index) if (parseInt(law.value) === index) if (parseInt(law) === index)

Still no results 😭😢😫

Also you said law isn't the loop variable but it's inside the loop I don't get it

Steven Parker
Steven Parker
231,275 Points

The "loop variable" is the one that is created inside the "for" initialization clause. It stores the iteration number and is incremented each time the loop repeats. It's not a property of "law".

Robert Young
Robert Young
11,579 Points

John Cannon This threw me off so damn hard - I was thinking 'it can't be i === index as it seemed too obvious... arrgg thanks Steven Parker