JavaScript DOM Scripting By Example Editing and Filtering Names Fix DOM Manipulation Code

Colin Bondi
Colin Bondi
8,005 Points

Stumped on the If condition of this code challenge.

For the if statement I am testing if the value entered stored in the index variable is equal to the current li item stored in the law variable. This is not correct so I can't see what I should be testing here?

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

           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

Hi Colin!

You are on the right track, for sure...

In your if statement, you want to be comparing apples to apples, but unfortunately, you are currently sort of attempting to compare apples to oranges.

index is an int value (apple)

i is an int value (apple)

laws[i] assigned to law is a list item element (orange)

So instead of this:

if ( index === law ) {

You want this:

if ( index === i ) {

Which passes.

Another way to think of it is, for each iteration of the loop, if that interation's index (i) matches the input variable (index), then embolden that list item.

I hope that helps.

Stay safe and happy coding!

Colin Bondi
Colin Bondi
8,005 Points

Hey Peter Thanks for your help with this and that certainly makes it clearer. I think I need a lot more practice with this because its still a bit confusing...

Hey Colin!

Just stick with it. It gets easier over time.

And tap into all the 3rd party resources you can, such as:

https://www.w3schools.com/

https://developer.mozilla.org/en-US/

https://www.freecodecamp.org/

https://www.youtube.com/results?search_query=derek+banas

https://css-tricks.com/

https://javascript.info/

And, of course, there's always Google and Stack Overflow!?!

I hope that helps.

Stay safe and happy coding!

Colin Bondi
Colin Bondi
8,005 Points

Helpful suggestions Thanks Peter!