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>

3 Answers

Steven Parker
Steven Parker
171,904 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
171,904 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.

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
171,904 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".

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

Thanks

Steven Parker
Steven Parker
171,904 Points

John Cannon — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!