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?

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

  = 'bold';
       } else {
  = 'normal';
<!DOCTYPE html>
  <title>Newton's Laws</title>
  <h1>Newton's Laws of Motion</h1>
    <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>
  <input type="text" id="boldIndex">
  <button id="embolden">Embolden</button>
  <script src="app.js"></script>

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:

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!